8#include <private/qsgcurveprocessor_p.h>
9#include <private/qquickshape_p.h>
10#include <private/qquadpath_p.h>
11#include <private/qquickitem_p.h>
12#include <private/qquickimagebase_p_p.h>
13#include <private/qquicktext_p.h>
14#include <private/qquicktranslate_p.h>
15#include <private/qquickimage_p.h>
17#include <QtCore/qloggingcategory.h>
18#include <QtCore/qdir.h>
19#include <QtCore/qstandardpaths.h>
23using namespace Qt::StringLiterals;
29 s.replace(QLatin1Char(
'\\'), QLatin1String(
"\\\\"));
30 s.replace(QLatin1Char(
'"'), QLatin1String(
"\\\""));
35QQuickAnimatedProperty::PropertyAnimation QQuickAnimatedProperty::PropertyAnimation::simplified()
const
37 QQuickAnimatedProperty::PropertyAnimation res = *
this;
38 int consecutiveEquals = 0;
39 int prevTimePoint = -1;
41 for (
const auto &[timePoint, value] : frames.asKeyValueRange()) {
42 if (value != prevValue) {
43 consecutiveEquals = 1;
45 }
else if (consecutiveEquals < 2) {
49 res.frames.remove(prevTimePoint);
50 res.easingPerFrame.remove(prevTimePoint);
52 prevTimePoint = timePoint;
58QQuickQmlGenerator::QQuickQmlGenerator(
const QString fileName, QQuickVectorImageGenerator::GeneratorFlags flags,
const QString &outFileName)
59 : QQuickGenerator(fileName, flags)
60 , outputFileName(outFileName)
62 m_result.open(QIODevice::ReadWrite);
63 m_oldIndentLevels.push(0);
65 if (outFileName.isEmpty()) {
66 setRetainFilePaths(
true);
67 setAssetFileDirectory(QStandardPaths::writableLocation(QStandardPaths::TempLocation));
68 setAssetFilePrefix(QStringLiteral(
"_qt_vectorimage_"));
69 setUrlPrefix(QStringLiteral(
"file:"));
73QQuickQmlGenerator::~QQuickQmlGenerator()
77bool QQuickQmlGenerator::save()
79 if (Q_UNLIKELY(errorState()))
83 if (!outputFileName.isEmpty()) {
84 QFileInfo fileInfo(outputFileName);
85 QDir dir(fileInfo.absolutePath());
86 if (!dir.exists() && !dir.mkpath(QStringLiteral(
"."))) {
87 qCWarning(lcQuickVectorImage) <<
"Failed to create path" << dir.absolutePath();
90 QFile outFile(outputFileName);
91 if (outFile.open(QIODevice::WriteOnly)) {
92 outFile.write(m_result.data());
95 qCWarning(lcQuickVectorImage) <<
"Failed to write to file" << outFile.fileName();
101 if (lcQuickVectorImage().isDebugEnabled())
102 qCDebug(lcQuickVectorImage).noquote() << m_result.data().left(300);
107void QQuickQmlGenerator::setShapeTypeName(
const QString &name)
109 m_shapeTypeName = name.toLatin1();
112QString QQuickQmlGenerator::shapeTypeName()
const
114 return QString::fromLatin1(m_shapeTypeName);
117void QQuickQmlGenerator::setCommentString(
const QString commentString)
119 m_commentString = commentString;
122QString QQuickQmlGenerator::commentString()
const
124 return m_commentString;
127QString QQuickQmlGenerator::generateNodeBase(
const NodeInfo &info,
const QString &idSuffix)
129 static qint64 maxNodes = qEnvironmentVariableIntegerValue(
"QT_QUICKVECTORIMAGE_MAX_NODES").value_or(10000);
130 if (Q_UNLIKELY(!checkSanityLimit(++m_nodeCounter, maxNodes,
"nodes"_L1)))
133 if (!info.nodeId.isEmpty())
134 stream() <<
"objectName: \"" << info.nodeId <<
"\"";
136 if (!info.id.isEmpty())
137 stream() <<
"id: " << info.id << idSuffix;
139 if (!info.bounds.isNull() || !info.boundsReferenceId.isEmpty()) {
140 stream() <<
"property var originalBounds: ";
141 if (!info.bounds.isNull()) {
142 stream(SameLine) <<
"Qt.rect(" << info.bounds.x() <<
", " << info.bounds.y() <<
", "
143 << info.bounds.width() <<
", " << info.bounds.height() <<
")";
145 stream(SameLine) << info.boundsReferenceId <<
".originalBounds";
147 stream() <<
"width: originalBounds.width";
148 stream() <<
"height: originalBounds.height";
151 stream() <<
"transformOrigin: Item.TopLeft";
153 if (info.filterId.isEmpty() && info.maskId.isEmpty()) {
154 if (!info.isDefaultOpacity)
155 stream() <<
"opacity: " << info.opacity.defaultValue().toReal();
156 generateItemAnimations(info.id, info);
162void QQuickQmlGenerator::generateNodeEnd(
const NodeInfo &info)
164 if (Q_UNLIKELY(errorState()))
168 generateShaderUse(info);
171void QQuickQmlGenerator::generateItemAnimations(
const QString &idString,
const NodeInfo &info)
173 const bool hasTransform = info.transform.isAnimated()
174 || !info.maskId.isEmpty()
175 || !info.filterId.isEmpty()
176 || !info.isDefaultTransform
177 || !info.transformReferenceId.isEmpty()
178 || info.motionPath.isAnimated();
181 stream() <<
"transform: TransformGroup {";
184 bool hasNonConstantTransform =
false;
185 int earliestOverrideGroup = -1;
187 if (!idString.isEmpty()) {
188 stream() <<
"id: " << idString <<
"_transform_base_group";
190 if (!info.maskId.isEmpty() || !info.filterId.isEmpty())
191 stream() <<
"Translate { x: " << idString <<
".sourceX; y: " << idString <<
".sourceY }";
193 if (info.transform.isAnimated()) {
194 for (
int groupIndex = 0; groupIndex < info.transform.animationGroupCount(); ++groupIndex) {
195 stream() <<
"TransformGroup {";
198 if (!idString.isEmpty())
199 stream() <<
"id: " << idString <<
"_transform_group_" << groupIndex;
201 int animationStart = info.transform.animationGroup(groupIndex);
202 int nextAnimationStart = groupIndex + 1 < info.transform.animationGroupCount()
203 ? info.transform.animationGroup(groupIndex + 1)
204 : info.transform.animationCount();
206 const QQuickAnimatedProperty::PropertyAnimation &firstAnimation = info.transform.animation(animationStart);
207 const bool replace = firstAnimation.flags & QQuickAnimatedProperty::PropertyAnimation::ReplacePreviousAnimations;
208 if (replace && earliestOverrideGroup < 0)
209 earliestOverrideGroup = groupIndex;
211 for (
int i = nextAnimationStart - 1; i >= animationStart; --i) {
212 const QQuickAnimatedProperty::PropertyAnimation &animation = info.transform.animation(i);
213 if (animation.frames.isEmpty())
216 const QVariantList ¶meters = animation.frames.first().value<QVariantList>();
217 switch (animation.subtype) {
218 case QTransform::TxTranslate:
219 if (animation.isConstant()) {
220 const QPointF translation = parameters.value(0).value<QPointF>();
221 if (!translation.isNull())
222 stream() <<
"Translate { x: " << translation.x() <<
"; y: " << translation.y() <<
" }";
224 hasNonConstantTransform =
true;
225 stream() <<
"Translate { id: " << idString <<
"_transform_" << groupIndex <<
"_" << i <<
" }";
228 case QTransform::TxScale:
229 if (animation.isConstant()) {
230 const QPointF scale = parameters.value(0).value<QPointF>();
231 if (scale != QPointF(1, 1))
232 stream() <<
"Scale { xScale: " << scale.x() <<
"; yScale: " << scale.y() <<
" }";
234 hasNonConstantTransform =
true;
235 stream() <<
"Scale { id: " << idString <<
"_transform_" << groupIndex <<
"_" << i <<
"}";
238 case QTransform::TxRotate:
239 if (animation.isConstant()) {
240 const QPointF center = parameters.value(0).value<QPointF>();
241 const qreal angle = parameters.value(1).toReal();
242 if (!qFuzzyIsNull(angle))
243 stream() <<
"Rotation { angle: " << angle <<
"; origin.x: " << center.x() <<
"; origin.y: " << center.y() <<
" }";
245 hasNonConstantTransform =
true;
246 stream() <<
"Rotation { id: " << idString <<
"_transform_" << groupIndex <<
"_" << i <<
" }";
249 case QTransform::TxShear:
250 if (animation.isConstant()) {
251 const QPointF skew = parameters.value(0).value<QPointF>();
253 stream() <<
"Shear { xAngle: " << skew.x() <<
"; yAngle: " << skew.y() <<
" }";
255 hasNonConstantTransform =
true;
256 stream() <<
"Shear { id: " << idString <<
"_transform_" << groupIndex <<
"_" << i <<
" }";
269 if (info.motionPath.isAnimated()) {
270 QVariantList defaultProps = info.motionPath.defaultValue().value<QVariantList>();
271 const bool adaptAngle = defaultProps.value(1).toBool();
272 const qreal baseRotation = defaultProps.value(2).toReal();
273 QString interpolatorId = idString + QStringLiteral(
"_motion_interpolator");
274 if (adaptAngle || !qFuzzyIsNull(baseRotation)) {
275 stream() <<
"Rotation {";
279 stream() <<
"angle: " << interpolatorId <<
".angle";
280 if (!qFuzzyIsNull(baseRotation))
281 stream(SameLine) <<
" + " << baseRotation;
283 stream() <<
"angle: " << baseRotation;
290 stream() <<
"Translate {";
293 stream() <<
"x: " << interpolatorId <<
".x";
294 stream() <<
"y: " << interpolatorId <<
".y";
301 if (!info.isDefaultTransform) {
302 QTransform xf = info.transform.defaultValue().value<QTransform>();
303 if (xf.type() <= QTransform::TxTranslate) {
304 stream() <<
"Translate { x: " << xf.dx() <<
"; y: " << xf.dy() <<
"}";
306 stream() <<
"Matrix4x4 { matrix: ";
307 generateTransform(xf);
308 stream(SameLine) <<
"}";
312 if (!info.transformReferenceId.isEmpty())
313 stream() <<
"Matrix4x4 { matrix: " << info.transformReferenceId <<
".transformMatrix }";
318 if (hasNonConstantTransform) {
319 generateAnimateTransform(idString, info);
320 }
else if (info.transform.isAnimated() && earliestOverrideGroup >= 0) {
323 stream() <<
"Component.onCompleted: {";
326 stream() << idString <<
"_transform_base_group.activateOverride("
327 << idString <<
"_transform_group_" << earliestOverrideGroup <<
")";
334 generateAnimateMotionPath(idString, info.motionPath);
336 generatePropertyAnimation(info.opacity, idString, QStringLiteral(
"opacity"));
339void QQuickQmlGenerator::generateShaderUse(
const NodeInfo &info)
341 const bool hasMask = !info.maskId.isEmpty();
342 const bool hasFilters = !info.filterId.isEmpty();
343 if (!hasMask && !hasFilters)
346 const QString effectId = hasFilters
347 ? info.filterId + QStringLiteral(
"_") + info.id + QStringLiteral(
"_effect")
350 QString animatedItemId;
352 stream() <<
"ShaderEffectSource {";
355 const QString seId = info.id + QStringLiteral(
"_se");
356 stream() <<
"id: " << seId;
358 stream() <<
"ItemSpy {";
360 stream() <<
"id: " << info.id <<
"_itemspy";
361 stream() <<
"anchors.fill: parent";
365 stream() <<
"hideSource: true";
366 stream() <<
"wrapMode: " << info.filterId <<
"_filterParameters.wrapMode";
367 stream() <<
"sourceItem: " << info.id;
368 stream() <<
"sourceRect: " << info.filterId
369 <<
"_filterParameters.adaptToFilterRect("
370 << info.id <<
".originalBounds.x, "
371 << info.id <<
".originalBounds.y, "
372 << info.id <<
".originalBounds.width, "
373 << info.id <<
".originalBounds.height)";
374 stream() <<
"textureSize: " << info.id <<
"_itemspy.requiredTextureSize";
375 stream() <<
"width: sourceRect.width";
376 stream() <<
"height: sourceRect.height";
377 stream() <<
"visible: false";
382 stream() <<
"Loader {";
385 animatedItemId = effectId;
386 stream() <<
"id: " << effectId;
388 stream() <<
"property var filterSourceItem: " << seId;
389 stream() <<
"sourceComponent: " << info.filterId <<
"_container";
390 stream() <<
"property real sourceX: " << info.id <<
".originalBounds.x";
391 stream() <<
"property real sourceY: " << info.id <<
".originalBounds.y";
392 stream() <<
"width: " << info.id <<
".originalBounds.width";
393 stream() <<
"height: " << info.id <<
".originalBounds.height";
403 stream() <<
"ShaderEffectSource {";
406 const QString maskId = info.maskId + QStringLiteral(
"_") + info.id + QStringLiteral(
"_mask");
407 stream() <<
"id: " << maskId;
408 stream() <<
"sourceItem: " << info.maskId;
409 stream() <<
"visible: false";
410 stream() <<
"hideSource: true";
412 if (m_contentRelativeMasks.contains(info.maskId)) {
413 stream() <<
"Binding { target: " << info.maskId
414 <<
"; property: \"contentX\"; value: " << info.id <<
".originalBounds.x }";
415 stream() <<
"Binding { target: " << info.maskId
416 <<
"; property: \"contentY\"; value: " << info.id <<
".originalBounds.y }";
417 stream() <<
"Binding { target: " << info.maskId
418 <<
"; property: \"contentWidth\"; value: " << info.id
419 <<
".originalBounds.width }";
420 stream() <<
"Binding { target: " << info.maskId
421 <<
"; property: \"contentHeight\"; value: " << info.id
422 <<
".originalBounds.height }";
425 stream() <<
"ItemSpy {";
427 stream() <<
"id: " << maskId <<
"_itemspy";
428 stream() <<
"anchors.fill: parent";
431 stream() <<
"textureSize: " << maskId <<
"_itemspy.requiredTextureSize";
433 stream() <<
"sourceRect: " << info.maskId <<
".maskRect("
434 << info.id <<
".originalBounds.x,"
435 << info.id <<
".originalBounds.y,"
436 << info.id <<
".originalBounds.width,"
437 << info.id <<
".originalBounds.height)";
439 stream() <<
"width: sourceRect.width";
440 stream() <<
"height: sourceRect.height";
446 stream() <<
"ShaderEffectSource {";
449 const QString seId = info.id + QStringLiteral(
"_masked_se");
450 stream() <<
"id: " << seId;
452 stream() <<
"ItemSpy {";
454 stream() <<
"id: " << info.id <<
"_masked_se_itemspy";
455 stream() <<
"anchors.fill: parent";
459 stream() <<
"hideSource: true";
461 stream() <<
"sourceItem: " << effectId;
463 stream() <<
"sourceItem: " << info.id;
464 stream() <<
"textureSize: " << info.id <<
"_masked_se_itemspy.requiredTextureSize";
466 stream() <<
"sourceRect: " << info.maskId <<
".maskRect("
467 << info.id <<
".originalBounds.x,"
468 << info.id <<
".originalBounds.y,"
469 << info.id <<
".originalBounds.width,"
470 << info.id <<
".originalBounds.height)";
472 stream() <<
"sourceRect: " << info.maskId <<
".maskRect(0, 0,"
473 << info.id <<
".originalBounds.width,"
474 << info.id <<
".originalBounds.height)";
476 stream() <<
"width: sourceRect.width";
477 stream() <<
"height: sourceRect.height";
478 stream() <<
"smooth: false";
479 stream() <<
"visible: false";
484 stream() <<
"ShaderEffect {";
487 const QString maskShaderId = maskId + QStringLiteral(
"_se");
488 animatedItemId = maskShaderId;
490 stream() <<
"id:" << maskShaderId;
492 stream() <<
"property real sourceX: " << maskId <<
".sourceRect.x";
493 stream() <<
"property real sourceY: " << maskId <<
".sourceRect.y";
494 stream() <<
"width: " << maskId <<
".sourceRect.width";
495 stream() <<
"height: " << maskId <<
".sourceRect.height";
497 stream() <<
"fragmentShader: \"qrc:/qt-project.org/quickvectorimage/helpers/shaders_ng/genericmask.frag.qsb\"";
498 stream() <<
"property var source: " << seId;
499 stream() <<
"property var maskSource: " << maskId;
500 stream() <<
"property bool isAlpha: " << (info.isMaskAlpha ?
"true" :
"false");
501 stream() <<
"property bool isInverted: " << (info.isMaskInverted ?
"true" :
"false");
504 if (!info.isDefaultOpacity)
505 stream() <<
"opacity: " << info.opacity.defaultValue().toReal();
507 generateItemAnimations(animatedItemId, info);
513bool QQuickQmlGenerator::generateDefsNode(
const StructureNodeInfo &info)
515 if (Q_UNLIKELY(errorState()))
518 if (info.stage == StructureNodeStage::Start) {
519 m_oldIndentLevels.push(m_indentLevel);
521 stream() <<
"Component {";
524 stream() <<
"id: " << info.id <<
"_container";
526 stream() <<
"Item {";
529 generateTimelineFields(info);
530 if (!info.transformReferenceChildId.isEmpty()) {
531 stream() <<
"property alias transformMatrix: "
532 << info.transformReferenceChildId <<
".transformMatrix";
535 generateNodeBase(info, QStringLiteral(
"_defs"));
537 generateNodeEnd(info);
542 stream() << m_defsSuffix;
543 m_defsSuffix.clear();
545 m_indentLevel = m_oldIndentLevels.pop();
551void QQuickQmlGenerator::generateImageNode(
const ImageNodeInfo &info)
553 if (Q_UNLIKELY(errorState() || !isNodeVisible(info)))
556 const QFileInfo outputFileInfo(outputFileName);
557 const QDir outputDir(outputFileInfo.absolutePath());
561 if (!m_retainFilePaths || info.externalFileReference.isEmpty()) {
562 filePath = m_assetFileDirectory;
563 if (filePath.isEmpty())
564 filePath = outputDir.absolutePath();
566 if (!filePath.isEmpty() && !filePath.endsWith(u'/'))
569 QDir fileDir(filePath);
570 if (!fileDir.exists()) {
571 if (!fileDir.mkpath(QStringLiteral(
".")))
572 qCWarning(lcQuickVectorImage) <<
"Failed to create image resource directory:" << filePath;
575 filePath += QStringLiteral(
"%1%2.png").arg(m_assetFilePrefix.isEmpty()
576 ? QStringLiteral(
"svg_asset_")
578 .arg(info.image.cacheKey());
580 if (!info.image.save(filePath))
581 qCWarning(lcQuickVectorImage) <<
"Unabled to save image resource" << filePath;
582 qCDebug(lcQuickVectorImage) <<
"Saving copy of IMAGE" << filePath;
584 filePath = info.externalFileReference;
587 const QFileInfo assetFileInfo(filePath);
589 stream() <<
"Image {";
592 generateNodeBase(info);
593 stream() <<
"x: " << info.rect.x();
594 stream() <<
"y: " << info.rect.y();
595 stream() <<
"width: " << info.rect.width();
596 stream() <<
"height: " << info.rect.height();
597 stream() <<
"source: \"" << m_urlPrefix << outputDir.relativeFilePath(assetFileInfo.absoluteFilePath()) <<
"\"";
598 generateNodeEnd(info);
601void QQuickQmlGenerator::generateMarkers(
const PathNodeInfo &info)
603 const QPainterPath path = info.path.defaultValue().value<QPainterPath>();
604 for (
int i = 0; i < path.elementCount(); ++i) {
605 const QPainterPath::Element element = path.elementAt(i);
610 auto getMeanAngle = [](QPointF p0, QPointF p1, QPointF p2) -> qreal {
611 QPointF t1 = p1 - p0;
612 QPointF t2 = p2 - p1;
613 qreal hyp1 = hypot(t1.x(), t1.y());
618 qreal hyp2 = hypot(t2.x(), t2.y());
623 QPointF tangent = t1 + t2;
624 return -atan2(tangent.y(), tangent.x()) / M_PI * 180.;
628 markerId = info.markerStartId;
629 angle = path.angleAtPercent(0.0);
630 }
else if (i == path.elementCount() - 1) {
631 markerId = info.markerEndId;
632 angle = path.angleAtPercent(1.0);
633 }
else if (path.elementAt(i + 1).type != QPainterPath::CurveToDataElement) {
634 markerId = info.markerMidId;
636 const QPainterPath::Element prevElement = path.elementAt(i - 1);
637 const QPainterPath::Element nextElement = path.elementAt(i + 1);
639 QPointF p1(prevElement.x, prevElement.y);
640 QPointF p2(element.x, element.y);
641 QPointF p3(nextElement.x, nextElement.y);
643 angle = getMeanAngle(p1, p2, p3);
646 if (!markerId.isEmpty()) {
647 stream() <<
"Loader {";
651 stream() <<
"sourceComponent: " << markerId <<
"_container";
652 stream() <<
"property real strokeWidth: " << info.strokeStyle.width.defaultValue().toReal();
653 stream() <<
"transform: [";
656 stream() <<
"Scale { "
657 <<
"xScale: " << markerId <<
"_markerParameters.startReversed ? -1 : 1; "
658 <<
"yScale: " << markerId <<
"_markerParameters.startReversed ? -1 : 1 },";
660 stream() <<
"Rotation { angle: " << markerId <<
"_markerParameters.autoAngle(" << -angle <<
") },";
661 stream() <<
"Translate { x: " << element.x <<
"; y: " << element.y <<
"}";
672void QQuickQmlGenerator::generatePath(
const PathNodeInfo &info,
const QRectF &overrideBoundingRect)
674 if (Q_UNLIKELY(errorState() || !isNodeVisible(info)))
677 if (m_inShapeItemLevel > 0) {
678 if (!info.isDefaultTransform)
679 qWarning() <<
"Skipped transform for node" << info.nodeId <<
"type" << info.typeName <<
"(this is not supposed to happen)";
680 optimizePaths(info, overrideBoundingRect);
682 m_inShapeItemLevel++;
683 stream() << shapeName() <<
" {";
686 generateNodeBase(info);
688 if (m_flags.testFlag(QQuickVectorImageGenerator::GeneratorFlag::CurveRenderer))
689 stream() <<
"preferredRendererType: Shape.CurveRenderer";
690 if (m_flags.testFlag(QQuickVectorImageGenerator::GeneratorFlag::AsyncShapes))
691 stream() <<
"asynchronous: true";
692 optimizePaths(info, overrideBoundingRect);
695 if (!info.markerStartId.isEmpty()
696 || !info.markerMidId.isEmpty()
697 || !info.markerEndId.isEmpty()) {
698 generateMarkers(info);
701 generateNodeEnd(info);
702 m_inShapeItemLevel--;
706void QQuickQmlGenerator::generateGradient(
const QGradient *grad,
707 const QString &propertyName,
708 const QRectF &coordinateConversion)
710 const QSizeF &scale = coordinateConversion.size();
711 const QPointF &translation = coordinateConversion.topLeft();
713 if (grad->type() == QGradient::LinearGradient) {
714 auto *linGrad =
static_cast<
const QLinearGradient *>(grad);
715 stream() << propertyName <<
": LinearGradient {";
718 QRectF gradRect(linGrad->start(), linGrad->finalStop());
720 stream() <<
"x1: " << (gradRect.left() * scale.width()) + translation.x();
721 stream() <<
"y1: " << (gradRect.top() * scale.height()) + translation.y();
722 stream() <<
"x2: " << (gradRect.right() * scale.width()) + translation.x();
723 stream() <<
"y2: " << (gradRect.bottom() * scale.height()) + translation.y();
724 for (
auto &stop : linGrad->stops())
725 stream() <<
"GradientStop { position: " << QString::number(stop.first,
'g', 7)
726 <<
"; color: \"" << stop.second.name(QColor::HexArgb) <<
"\" }";
727 }
else if (grad->type() == QGradient::RadialGradient) {
728 auto *radGrad =
static_cast<
const QRadialGradient*>(grad);
729 stream() << propertyName <<
": RadialGradient {";
732 stream() <<
"centerX: " << (radGrad->center().x() * scale.width()) + translation.x();
733 stream() <<
"centerY: " << (radGrad->center().y() * scale.height()) + translation.y();
734 stream() <<
"centerRadius: " << (radGrad->radius() * scale.width());
735 stream() <<
"focalX:" << (radGrad->focalPoint().x() * scale.width()) + translation.x();
736 stream() <<
"focalY:" << (radGrad->focalPoint().y() * scale.height()) + translation.y();
737 for (
auto &stop : radGrad->stops())
738 stream() <<
"GradientStop { position: " << QString::number(stop.first,
'g', 7)
739 <<
"; color: \"" << stop.second.name(QColor::HexArgb) <<
"\" }";
742 stream() <<
"spread: ShapeGradient.";
743 switch (grad->spread()) {
744 case QGradient::PadSpread:
745 stream(SameLine) <<
"PadSpread";
747 case QGradient::ReflectSpread:
748 stream(SameLine) <<
"ReflectSpread";
750 case QGradient::RepeatSpread:
751 stream(SameLine) <<
"RepeatSpread";
759void QQuickQmlGenerator::generateAnimationBindings()
762 if (Q_UNLIKELY(!isRuntimeGenerator()))
763 prefix = QStringLiteral(
".animations");
765 stream() <<
"loops: " << m_topLevelIdString << prefix <<
".loops";
766 stream() <<
"paused: " << m_topLevelIdString << prefix <<
".paused";
767 stream() <<
"running: true";
770 stream() <<
"onLoopsChanged: { if (running) { restart() } }";
773void QQuickQmlGenerator::generateEasing(
const QQuickAnimatedProperty::PropertyAnimation &animation,
774 int time,
int streamFlags)
776 if (animation.easingPerFrame.contains(time)) {
777 QBezier bezier = animation.easingPerFrame.value(time);
778 QPointF c1 = bezier.pt2();
779 QPointF c2 = bezier.pt3();
781 bool isLinear = (c1 == c1.transposed() && c2 == c2.transposed());
783 int nextIdx = m_easings.size();
784 QString &id = m_easings[{c1.x(), c1.y(), c2.x(), c2.y()}];
786 id = QString(QLatin1String(
"easing_%1")).arg(nextIdx, 2, 10, QLatin1Char(
'0'));
787 if (streamFlags & SameLine)
788 stream(streamFlags) <<
"; ";
789 stream(streamFlags) <<
"easing: " << m_topLevelIdString <<
"." << id;
794void QQuickQmlGenerator::generatePropertyAnimation(
const QQuickAnimatedProperty &property,
795 const QString &targetName,
796 const QString &propertyName,
797 AnimationType animationType)
799 if (!property.isAnimated())
802 if (usingTimelineAnimation())
803 return generatePropertyTimeline(property, targetName, propertyName, animationType);
805 QString mainAnimationId = targetName
806 + QStringLiteral(
"_")
808 + QStringLiteral(
"_animation");
809 mainAnimationId.replace(QLatin1Char(
'.'), QLatin1Char(
'_'));
812 if (Q_UNLIKELY(!isRuntimeGenerator()))
813 prefix = QStringLiteral(
".animations");
815 stream() <<
"Connections { target: " << m_topLevelIdString << prefix <<
"; function onRestart() {" << mainAnimationId <<
".restart() } }";
817 stream() <<
"ParallelAnimation {";
820 stream() <<
"id: " << mainAnimationId;
822 generateAnimationBindings();
824 for (
int i = 0; i < property.animationCount(); ++i) {
825 const QQuickAnimatedProperty::PropertyAnimation &animation = property.animation(i);
827 stream() <<
"SequentialAnimation {";
830 const int startOffset = processAnimationTime(animation.startOffset);
832 stream() <<
"PauseAnimation { duration: " << startOffset <<
" }";
834 stream() <<
"SequentialAnimation {";
837 const int repeatCount = animation.repeatCount;
839 stream() <<
"loops: Animation.Infinite";
841 stream() <<
"loops: " << repeatCount;
843 int previousTime = 0;
844 QVariant previousValue;
845 for (
auto it = animation.frames.constBegin(); it != animation.frames.constEnd(); ++it) {
846 const int time = it.key();
847 const int frameTime = processAnimationTime(time - previousTime);
848 const QVariant &value = it.value();
850 if (previousValue.isValid() && previousValue == value) {
852 stream() <<
"PauseAnimation { duration: " << frameTime <<
" }";
853 }
else if (animationType == AnimationType::Auto && value.typeId() == QMetaType::Bool) {
856 stream() <<
"PauseAnimation { duration: " << frameTime <<
" }";
857 stream() <<
"ScriptAction {";
860 stream() <<
"script:" << targetName <<
"." << propertyName <<
" = " << value.toString();
865 generateAnimatedPropertySetter(targetName,
875 previousValue = value;
878 if (!(animation.flags & QQuickAnimatedProperty::PropertyAnimation::FreezeAtEnd)) {
879 stream() <<
"ScriptAction {";
881 stream() <<
"script: ";
883 switch (animationType) {
884 case AnimationType::Auto:
885 stream(SameLine) << targetName <<
"." << propertyName <<
" = ";
887 case AnimationType::ColorOpacity:
888 stream(SameLine) << targetName <<
"." << propertyName <<
".a = ";
892 QVariant value = property.defaultValue();
893 if (value.typeId() == QMetaType::QColor)
894 stream(SameLine) <<
"\"" << value.toString() <<
"\"";
896 stream(SameLine) << value.toReal();
913void QQuickQmlGenerator::generateTimelinePropertySetter(
914 const QString &targetName,
915 const QString &propertyName,
916 const QQuickAnimatedProperty::PropertyAnimation &animation,
917 std::function<QVariant(
const QVariant &)>
const& extractValue,
920 if (animation.repeatCount != 1 || animation.startOffset
921 || animation.flags != QQuickAnimatedProperty::PropertyAnimation::FreezeAtEnd) {
922 qCWarning(lcQuickVectorImage) <<
"Animation feature not implemented in timeline mode, for"
923 << targetName << propertyName;
926 stream() <<
"KeyframeGroup {";
928 stream() <<
"target: " << targetName;
929 stream() <<
"property: \"" << propertyName <<
"\"";
931 for (
const auto &[frame, rawValue] : animation.frames.asKeyValueRange()) {
933 if (rawValue.typeId() == QMetaType::QVariantList)
934 value = extractValue(rawValue.toList().value(valueIndex));
936 value = extractValue(rawValue);
938 stream() <<
"Keyframe { frame: " << frame <<
"; value: ";
939 if (value.typeId() == QMetaType::QVector3D) {
940 const QVector3D &v = value.value<QVector3D>();
941 stream(SameLine) <<
"Qt.vector3d(" << v.x() <<
", " << v.y() <<
", " << v.z() <<
")";
942 }
else if (value.typeId() == QMetaType::QColor) {
943 stream(SameLine) <<
"\"" << value.toString() <<
"\"";
945 stream(SameLine) << value.toReal();
947 generateEasing(animation, frame, SameLine);
948 stream(SameLine) <<
" }";
955void QQuickQmlGenerator::generatePropertyTimeline(
const QQuickAnimatedProperty &property,
956 const QString &targetName,
957 const QString &propertyName,
958 AnimationType animationType)
960 if (animationType == QQuickQmlGenerator::AnimationType::ColorOpacity) {
961 qCWarning(lcQuickVectorImage) <<
"ColorOpacity animation not available in timeline mode";
965 if (property.animationGroupCount() > 1 || property.animationCount() > 1) {
966 qCWarning(lcQuickVectorImage) <<
"Property feature not implemented in timeline mode, for"
967 << targetName << propertyName;
970 stream() <<
"Timeline {";
972 stream() <<
"currentFrame: " << property.timelineReferenceId() <<
".frameCounter";
973 stream() <<
"enabled: true";
975 auto extractor = [](
const QVariant &value) {
return value; };
976 generateTimelinePropertySetter(targetName, propertyName, property.animation(0), extractor);
982void QQuickQmlGenerator::generateTransform(
const QTransform &xf)
985 stream(SameLine) <<
"PlanarTransform.fromAffineMatrix("
986 << xf.m11() <<
", " << xf.m12() <<
", "
987 << xf.m21() <<
", " << xf.m22() <<
", "
988 << xf.dx() <<
", " << xf.dy() <<
")";
991 stream(SameLine) <<
"Qt.matrix4x4(";
993 const auto *data = m.data();
994 for (
int i = 0; i < 4; i++) {
995 stream() << data[i] <<
", " << data[i+4] <<
", " << data[i+8] <<
", " << data[i+12];
997 stream(SameLine) <<
", ";
999 stream(SameLine) <<
")";
1004void QQuickQmlGenerator::outputShapePath(
const PathNodeInfo &info,
const QPainterPath *painterPath,
const QQuadPath *quadPath, QQuickVectorImageGenerator::PathSelector pathSelector,
const QRectF &boundingRect)
1006 Q_UNUSED(pathSelector)
1007 Q_ASSERT(painterPath || quadPath);
1009 if (Q_UNLIKELY(errorState()))
1012 const bool invalidGradientBounds = info.strokeGrad.coordinateMode() == QGradient::ObjectMode
1013 && (qFuzzyIsNull(boundingRect.width()) ||
1014 qFuzzyIsNull(boundingRect.height()));
1016 const QColor strokeColor = info.strokeStyle.color.defaultValue().value<QColor>();
1017 const bool noPen = (strokeColor == QColorConstants::Transparent || !strokeColor.isValid())
1018 && !info.strokeStyle.color.isAnimated()
1019 && !info.strokeStyle.opacity.isAnimated()
1020 && (info.strokeGrad.type() == QGradient::NoGradient
1021 || invalidGradientBounds);
1022 if (pathSelector == QQuickVectorImageGenerator::StrokePath && noPen)
1025 const QColor fillColor = info.fillColor.defaultValue().value<QColor>();
1026 const bool noFill = info.grad.type() == QGradient::NoGradient
1027 && fillColor == QColorConstants::Transparent
1028 && !info.fillColor.isAnimated()
1029 && !info.fillOpacity.isAnimated();
1030 if (pathSelector == QQuickVectorImageGenerator::FillPath && noFill)
1033 if (noPen && noFill)
1035 auto fillRule = QQuickShapePath::FillRule(painterPath ? painterPath->fillRule() : quadPath->fillRule());
1036 stream() <<
"ShapePath {";
1039 QString shapePathId = info.id;
1040 if (pathSelector & QQuickVectorImageGenerator::FillPath)
1041 shapePathId += QStringLiteral(
"_fill");
1042 if (pathSelector & QQuickVectorImageGenerator::StrokePath)
1043 shapePathId += QStringLiteral(
"_stroke");
1045 stream() <<
"id: " << shapePathId;
1047 if (!info.nodeId.isEmpty()) {
1048 switch (pathSelector) {
1049 case QQuickVectorImageGenerator::FillPath:
1050 stream() <<
"objectName: \"svg_fill_path:" << info.nodeId <<
"\"";
1052 case QQuickVectorImageGenerator::StrokePath:
1053 stream() <<
"objectName: \"svg_stroke_path:" << info.nodeId <<
"\"";
1055 case QQuickVectorImageGenerator::FillAndStroke:
1056 stream() <<
"objectName: \"svg_path:" << info.nodeId <<
"\"";
1061 if (noPen || !(pathSelector & QQuickVectorImageGenerator::StrokePath)) {
1062 stream() <<
"strokeColor: \"transparent\"";
1064 if (info.strokeGrad.type() != QGradient::NoGradient && !invalidGradientBounds) {
1065 QRectF coordinateSys = info.strokeGrad.coordinateMode() == QGradient::ObjectMode
1067 : QRectF(0.0, 0.0, 1.0, 1.0);
1068 generateGradient(&info.strokeGrad, QStringLiteral(
"strokeGradient"), coordinateSys);
1069 }
else if (info.strokeStyle.opacity.isAnimated()) {
1070 stream() <<
"property color strokeBase: \"" << strokeColor.name(QColor::HexArgb) <<
"\"";
1071 stream() <<
"property real strokeOpacity: " << info.strokeStyle.opacity.defaultValue().toReal();
1072 stream() <<
"strokeColor: Qt.rgba(strokeBase.r, strokeBase.g, strokeBase.b, strokeOpacity)";
1074 stream() <<
"strokeColor: \"" << strokeColor.name(QColor::HexArgb) <<
"\"";
1076 stream() <<
"strokeWidth: " << info.strokeStyle.width.defaultValue().toReal();
1077 stream() <<
"capStyle: " << QQuickVectorImageGenerator::Utils::strokeCapStyleString(info.strokeStyle.lineCapStyle);
1078 stream() <<
"joinStyle: " << QQuickVectorImageGenerator::Utils::strokeJoinStyleString(info.strokeStyle.lineJoinStyle);
1079 stream() <<
"miterLimit: " << info.strokeStyle.miterLimit;
1080 if (info.strokeStyle.cosmetic)
1081 stream() <<
"cosmeticStroke: true";
1082 if (info.strokeStyle.dashArray.length() != 0) {
1083 stream() <<
"strokeStyle: " <<
"ShapePath.DashLine";
1084 stream() <<
"dashPattern: " << QQuickVectorImageGenerator::Utils::listString(info.strokeStyle.dashArray);
1085 stream() <<
"dashOffset: " << info.strokeStyle.dashOffset.defaultValue().toReal();
1089 QTransform fillTransform = info.fillTransform;
1090 if (!(pathSelector & QQuickVectorImageGenerator::FillPath)) {
1091 stream() <<
"fillColor: \"transparent\"";
1092 }
else if (info.grad.type() != QGradient::NoGradient) {
1093 generateGradient(&info.grad, QStringLiteral(
"fillGradient"));
1096 if (info.grad.coordinateMode() == QGradient::ObjectMode) {
1097 QTransform objectToUserSpace;
1098 objectToUserSpace.translate(boundingRect.x(), boundingRect.y());
1099 objectToUserSpace.scale(boundingRect.width(), boundingRect.height());
1100 fillTransform *= objectToUserSpace;
1103 if (info.fillOpacity.isAnimated()) {
1104 stream() <<
"property color fillBase: \"" << fillColor.name(QColor::HexArgb) <<
"\"";
1105 stream() <<
"property real fillOpacity:" << info.fillOpacity.defaultValue().toReal();
1106 stream() <<
"fillColor: Qt.rgba(fillBase.r, fillBase.g, fillBase.b, fillOpacity)";
1108 stream() <<
"fillColor: \"" << fillColor.name(QColor::HexArgb) <<
"\"";
1112 if (!info.patternId.isEmpty()) {
1113 stream() <<
"fillItem: ShaderEffectSource {";
1116 stream() <<
"parent: " << info.id;
1117 stream() <<
"sourceItem: " << info.patternId;
1118 stream() <<
"hideSource: true";
1119 stream() <<
"visible: false";
1120 stream() <<
"width: " << info.patternId <<
".width";
1121 stream() <<
"height: " << info.patternId <<
".height";
1122 stream() <<
"wrapMode: ShaderEffectSource.Repeat";
1123 stream() <<
"textureSize: Qt.size(width * __qt_toplevel_scale_itemspy.requiredTextureSize.width, "
1124 <<
"height * __qt_toplevel_scale_itemspy.requiredTextureSize.height)";;
1125 stream() <<
"sourceRect: " << info.patternId <<
".sourceRect("
1126 << info.id <<
".width, "
1127 << info.id <<
".height)";
1134 stream() <<
"function calculateFillTransform(xScale, yScale) {";
1137 stream() <<
"var m = ";
1138 generateTransform(fillTransform);
1140 stream() <<
"m.translate(" << info.patternId <<
".sourceOffset("
1141 << info.id <<
".width, "
1142 << info.id <<
".height))";
1144 stream() <<
"m.scale(1.0 / xScale, 1.0 / yScale, 1.0)";
1145 stream() <<
"return m";
1150 stream() <<
"fillTransform: calculateFillTransform(__qt_toplevel_scale_itemspy.requiredTextureSize.width, "
1151 <<
"__qt_toplevel_scale_itemspy.requiredTextureSize.height)";
1153 }
else if (!fillTransform.isIdentity()) {
1154 const QTransform &xf = fillTransform;
1155 stream() <<
"fillTransform: ";
1156 if (info.fillTransform.type() == QTransform::TxTranslate)
1157 stream(SameLine) <<
"PlanarTransform.fromTranslate(" << xf.dx() <<
", " << xf.dy() <<
")";
1158 else if (info.fillTransform.type() == QTransform::TxScale && !xf.dx() && !xf.dy())
1159 stream(SameLine) <<
"PlanarTransform.fromScale(" << xf.m11() <<
", " << xf.m22() <<
")";
1161 generateTransform(xf);
1164 if (info.trim.enabled) {
1165 stream() <<
"trim.start: " << info.trim.start.defaultValue().toReal();
1166 stream() <<
"trim.end: " << info.trim.end.defaultValue().toReal();
1167 stream() <<
"trim.offset: " << info.trim.offset.defaultValue().toReal();
1171 if (fillRule == QQuickShapePath::WindingFill)
1172 stream() <<
"fillRule: ShapePath.WindingFill";
1174 stream() <<
"fillRule: ShapePath.OddEvenFill";
1178 hintStr = QQuickVectorImageGenerator::Utils::pathHintString(*quadPath);
1179 if (!hintStr.isEmpty())
1180 stream() << hintStr;
1182 QQuickAnimatedProperty pathFactor(QVariant::fromValue(0));
1183 pathFactor.setTimelineReferenceId(info.path.timelineReferenceId());
1184 QString pathId = shapePathId +
"_ip"_L1;
1185 if (!info.path.isAnimated() || (info.path.animation(0).startOffset == 0 && info.path.animation(0).isConstant())) {
1186 QString svgPathString = painterPath ? QQuickVectorImageGenerator::Utils::toSvgString(*painterPath) : QQuickVectorImageGenerator::Utils::toSvgString(*quadPath);
1187 stream() <<
"PathSvg { path: \"" << svgPathString <<
"\" }";
1189 stream() <<
"PathInterpolated {";
1191 stream() <<
"id: " << pathId;
1192 stream() <<
"svgPaths: [";
1194 QQuickAnimatedProperty::PropertyAnimation pathFactorAnim = info.path.animation(0);
1195 auto &frames = pathFactorAnim.frames;
1198 for (
auto it = frames.begin(); it != frames.end(); ++it) {
1199 QString svg = QQuickVectorImageGenerator::Utils::toSvgString(it->value<QPainterPath>());
1200 if (svg != lastSvg) {
1202 stream(SameLine) <<
",";
1203 stream() <<
"\"" << svg <<
"\"";
1207 *it = QVariant::fromValue(pathIdx);
1209 pathFactor.addAnimation(pathFactorAnim);
1219 if (pathFactor.isAnimated())
1220 generatePropertyAnimation(pathFactor, pathId,
"factor"_L1);
1222 if (info.trim.enabled) {
1223 generatePropertyAnimation(info.trim.start, shapePathId + QStringLiteral(
".trim"), QStringLiteral(
"start"));
1224 generatePropertyAnimation(info.trim.end, shapePathId + QStringLiteral(
".trim"), QStringLiteral(
"end"));
1225 generatePropertyAnimation(info.trim.offset, shapePathId + QStringLiteral(
".trim"), QStringLiteral(
"offset"));
1228 if (info.strokeStyle.opacity.isAnimated()) {
1229 generatePropertyAnimation(info.strokeStyle.color, shapePathId, QStringLiteral(
"strokeBase"));
1230 generatePropertyAnimation(info.strokeStyle.opacity, shapePathId, QStringLiteral(
"strokeOpacity"));
1232 generatePropertyAnimation(info.strokeStyle.color, shapePathId, QStringLiteral(
"strokeColor"));
1234 if (info.strokeStyle.width.isAnimated())
1235 generatePropertyAnimation(info.strokeStyle.width, shapePathId, QStringLiteral(
"strokeWidth"));
1236 if (info.strokeStyle.dashOffset.isAnimated())
1237 generatePropertyAnimation(info.strokeStyle.dashOffset, shapePathId, QStringLiteral(
"dashOffset"));
1239 if (info.fillOpacity.isAnimated()) {
1240 generatePropertyAnimation(info.fillColor, shapePathId, QStringLiteral(
"fillBase"));
1241 generatePropertyAnimation(info.fillOpacity, shapePathId, QStringLiteral(
"fillOpacity"));
1243 generatePropertyAnimation(info.fillColor, shapePathId, QStringLiteral(
"fillColor"));
1247void QQuickQmlGenerator::generateNode(
const NodeInfo &info)
1249 if (Q_UNLIKELY(errorState() || !isNodeVisible(info)))
1252 stream() <<
"// Missing Implementation for SVG Node: " << info.typeName;
1253 stream() <<
"// Adding an empty Item and skipping";
1254 stream() <<
"Item {";
1256 generateNodeBase(info);
1257 generateNodeEnd(info);
1260void QQuickQmlGenerator::generateTextNode(
const TextNodeInfo &info)
1262 if (Q_UNLIKELY(errorState() || !isNodeVisible(info)))
1265 stream() <<
"Item {";
1267 generateNodeBase(info);
1269 if (!info.isTextArea)
1270 stream() <<
"Item { id: textAlignItem_" << m_textNodeCounter <<
"; x: " << info.position.x() <<
"; y: " << info.position.y() <<
"}";
1272 stream() <<
"Text {";
1276 const QString textItemId = QStringLiteral(
"_qt_textItem_%1").arg(m_textNodeCounter);
1277 stream() <<
"id: " << textItemId;
1279 generatePropertyAnimation(info.fillColor, textItemId, QStringLiteral(
"color"));
1280 generatePropertyAnimation(info.fillOpacity, textItemId, QStringLiteral(
"color"), AnimationType::ColorOpacity);
1281 generatePropertyAnimation(info.strokeColor, textItemId, QStringLiteral(
"styleColor"));
1282 generatePropertyAnimation(info.strokeOpacity, textItemId, QStringLiteral(
"styleColor"), AnimationType::ColorOpacity);
1284 if (info.isTextArea) {
1285 stream() <<
"x: " << info.position.x();
1286 stream() <<
"y: " << info.position.y();
1287 if (info.size.width() > 0) {
1288 stream() <<
"width: " << info.size.width();
1289 stream() <<
"wrapMode: Text.Wrap";
1291 if (info.size.height() > 0)
1292 stream() <<
"height: " << info.size.height();
1293 stream() <<
"clip: true";
1295 QString hAlign = QStringLiteral(
"left");
1296 stream() <<
"anchors.baseline: textAlignItem_" << m_textNodeCounter <<
".top";
1297 switch (info.alignment) {
1298 case Qt::AlignHCenter:
1299 hAlign = QStringLiteral(
"horizontalCenter");
1301 case Qt::AlignRight:
1302 hAlign = QStringLiteral(
"right");
1305 qCDebug(lcQuickVectorImage) <<
"Unexpected text alignment" << info.alignment;
1310 stream() <<
"anchors." << hAlign <<
": textAlignItem_" << m_textNodeCounter <<
".left";
1312 m_textNodeCounter++;
1314 stream() <<
"color: \"" << info.fillColor.defaultValue().value<QColor>().name(QColor::HexArgb) <<
"\"";
1315 stream() <<
"textFormat:" << (info.needsRichText ?
"Text.RichText" :
"Text.StyledText");
1317 stream() <<
"text: \"" << sanitizeString(info.text) <<
"\"";
1318 stream() <<
"font.family: \"" << sanitizeString(info.font.family()) <<
"\"";
1319 if (info.font.pixelSize() > 0)
1320 stream() <<
"font.pixelSize:" << info.font.pixelSize();
1321 else if (info.font.pointSize() > 0)
1322 stream() <<
"font.pixelSize:" << info.font.pointSizeF();
1323 if (info.font.underline())
1324 stream() <<
"font.underline: true";
1325 if (info.font.weight() != QFont::Normal)
1326 stream() <<
"font.weight: " <<
int(info.font.weight());
1327 if (info.font.italic())
1328 stream() <<
"font.italic: true";
1329 switch (info.font.hintingPreference()) {
1330 case QFont::PreferFullHinting:
1331 stream() <<
"font.hintingPreference: Font.PreferFullHinting";
1333 case QFont::PreferVerticalHinting:
1334 stream() <<
"font.hintingPreference: Font.PreferVerticalHinting";
1336 case QFont::PreferNoHinting:
1337 stream() <<
"font.hintingPreference: Font.PreferNoHinting";
1339 case QFont::PreferDefaultHinting:
1340 stream() <<
"font.hintingPreference: Font.PreferDefaultHinting";
1344 const QColor strokeColor = info.strokeColor.defaultValue().value<QColor>();
1345 if (strokeColor != QColorConstants::Transparent || info.strokeColor.isAnimated()) {
1346 stream() <<
"styleColor: \"" << strokeColor.name(QColor::HexArgb) <<
"\"";
1347 stream() <<
"style: Text.Outline";
1353 generateNodeEnd(info);
1356void QQuickQmlGenerator::generateUseNode(
const UseNodeInfo &info)
1358 if (Q_UNLIKELY(errorState() || !isNodeVisible(info)))
1361 if (info.stage == StructureNodeStage::Start) {
1362 stream() <<
"Item {";
1364 generateNodeBase(info);
1366 generateNodeEnd(info);
1370void QQuickQmlGenerator::generatePathContainer(
const StructureNodeInfo &info)
1373 stream() << shapeName() <<
" {";
1375 if (m_flags.testFlag(QQuickVectorImageGenerator::GeneratorFlag::CurveRenderer))
1376 stream() <<
"preferredRendererType: Shape.CurveRenderer";
1377 if (m_flags.testFlag(QQuickVectorImageGenerator::GeneratorFlag::AsyncShapes))
1378 stream() <<
"asynchronous: true";
1381 m_inShapeItemLevel++;
1384void QQuickQmlGenerator::generateAnimateMotionPath(
const QString &targetName,
1385 const QQuickAnimatedProperty &property)
1387 if (!property.isAnimated())
1390 QPainterPath path = property.defaultValue().value<QVariantList>().value(0).value<QPainterPath>();
1391 const QString mainAnimationId = targetName + QStringLiteral(
"_motion_interpolator");
1392 stream() <<
"PathInterpolator {";
1394 stream() <<
"id: " << mainAnimationId;
1395 const QString svgPathString = QQuickVectorImageGenerator::Utils::toSvgString(path);
1396 stream() <<
"path: Path { PathSvg { path: \"" << svgPathString <<
"\" } }";
1400 generatePropertyAnimation(property, mainAnimationId, QStringLiteral(
"progress"));
1403void QQuickQmlGenerator::generateAnimatedPropertySetter(
const QString &targetName,
1404 const QString &propertyName,
1405 const QVariant &value,
1406 const QQuickAnimatedProperty::PropertyAnimation &animation,
1409 AnimationType animationType)
1411 if (frameTime > 0) {
1412 switch (animationType) {
1413 case AnimationType::Auto:
1414 if (value.typeId() == QMetaType::QColor)
1415 stream() <<
"ColorAnimation {";
1417 stream() <<
"PropertyAnimation {";
1419 case AnimationType::ColorOpacity:
1420 stream() <<
"ColorOpacityAnimation {";
1425 stream() <<
"duration: " << frameTime;
1426 stream() <<
"target: " << targetName;
1427 stream() <<
"property: \"" << propertyName <<
"\"";
1429 if (value.typeId() == QMetaType::QVector3D) {
1430 const QVector3D &v = value.value<QVector3D>();
1431 stream(SameLine) <<
"Qt.vector3d(" << v.x() <<
", " << v.y() <<
", " << v.z() <<
")";
1432 }
else if (value.typeId() == QMetaType::QColor) {
1433 stream(SameLine) <<
"\"" << value.toString() <<
"\"";
1435 stream(SameLine) << value.toReal();
1437 generateEasing(animation, time);
1441 stream() <<
"ScriptAction {";
1443 stream() <<
"script:" << targetName <<
"." << propertyName;
1444 if (animationType == AnimationType::ColorOpacity)
1445 stream(SameLine) <<
".a";
1447 stream(SameLine) <<
" = ";
1448 if (value.typeId() == QMetaType::QVector3D) {
1449 const QVector3D &v = value.value<QVector3D>();
1450 stream(SameLine) <<
"Qt.vector3d(" << v.x() <<
", " << v.y() <<
", " << v.z() <<
")";
1451 }
else if (value.typeId() == QMetaType::QColor) {
1452 stream(SameLine) <<
"\"" << value.toString() <<
"\"";
1454 stream(SameLine) << value.toReal();
1461void QQuickQmlGenerator::generateAnimateTransform(
const QString &targetName,
const NodeInfo &info)
1463 if (!info.transform.isAnimated())
1466 if (usingTimelineAnimation())
1467 return generateTransformTimeline(targetName, info);
1469 const QString mainAnimationId = targetName
1470 + QStringLiteral(
"_transform_animation");
1473 if (Q_UNLIKELY(!isRuntimeGenerator()))
1474 prefix = QStringLiteral(
".animations");
1475 stream() <<
"Connections { target: " << m_topLevelIdString << prefix <<
"; function onRestart() {" << mainAnimationId <<
".restart() } }";
1477 stream() <<
"ParallelAnimation {";
1480 stream() <<
"id:" << mainAnimationId;
1482 generateAnimationBindings();
1483 for (
int groupIndex = 0; groupIndex < info.transform.animationGroupCount(); ++groupIndex) {
1484 int animationStart = info.transform.animationGroup(groupIndex);
1485 int nextAnimationStart = groupIndex + 1 < info.transform.animationGroupCount()
1486 ? info.transform.animationGroup(groupIndex + 1)
1487 : info.transform.animationCount();
1490 const QQuickAnimatedProperty::PropertyAnimation &firstAnimation = info.transform.animation(animationStart);
1491 const bool freeze = firstAnimation.flags & QQuickAnimatedProperty::PropertyAnimation::FreezeAtEnd;
1492 const bool replace = firstAnimation.flags & QQuickAnimatedProperty::PropertyAnimation::ReplacePreviousAnimations;
1494 stream() <<
"SequentialAnimation {";
1497 const int startOffset = processAnimationTime(firstAnimation.startOffset);
1498 if (startOffset > 0)
1499 stream() <<
"PauseAnimation { duration: " << startOffset <<
" }";
1501 const int repeatCount = firstAnimation.repeatCount;
1502 if (repeatCount < 0)
1503 stream() <<
"loops: Animation.Infinite";
1505 stream() <<
"loops: " << repeatCount;
1508 stream() <<
"ScriptAction {";
1511 stream() <<
"script: " << targetName <<
"_transform_base_group"
1512 <<
".activateOverride(" << targetName <<
"_transform_group_" << groupIndex <<
")";
1518 stream() <<
"ParallelAnimation {";
1521 for (
int i = animationStart; i < nextAnimationStart; ++i) {
1522 const QQuickAnimatedProperty::PropertyAnimation &animation = info.transform.animation(i);
1523 if (animation.isConstant())
1525 bool hasRotationCenter =
false;
1526 if (animation.subtype == QTransform::TxRotate) {
1527 for (
auto it = animation.frames.constBegin(); it != animation.frames.constEnd(); ++it) {
1528 const QPointF center = it->value<QVariantList>().value(0).value<QPointF>();
1529 if (!center.isNull()) {
1530 hasRotationCenter =
true;
1536 stream() <<
"SequentialAnimation {";
1539 int previousTime = 0;
1540 QVariantList previousParameters;
1541 for (
auto it = animation.frames.constBegin(); it != animation.frames.constEnd(); ++it) {
1542 const int time = it.key();
1543 const int frameTime = processAnimationTime(time - previousTime);
1544 const QVariantList ¶meters = it.value().value<QVariantList>();
1545 if (parameters.isEmpty())
1548 if (parameters == previousParameters) {
1550 stream() <<
"PauseAnimation { duration: " << frameTime <<
" }";
1552 stream() <<
"ParallelAnimation {";
1555 const QString propertyTargetName = targetName
1556 + QStringLiteral(
"_transform_")
1557 + QString::number(groupIndex)
1558 + QStringLiteral(
"_")
1559 + QString::number(i);
1561 switch (animation.subtype) {
1562 case QTransform::TxTranslate:
1564 const QPointF translation = parameters.first().value<QPointF>();
1566 generateAnimatedPropertySetter(propertyTargetName,
1567 QStringLiteral(
"x"),
1572 generateAnimatedPropertySetter(propertyTargetName,
1573 QStringLiteral(
"y"),
1580 case QTransform::TxScale:
1582 const QPointF scale = parameters.first().value<QPointF>();
1583 generateAnimatedPropertySetter(propertyTargetName,
1584 QStringLiteral(
"xScale"),
1589 generateAnimatedPropertySetter(propertyTargetName,
1590 QStringLiteral(
"yScale"),
1597 case QTransform::TxRotate:
1599 Q_ASSERT(parameters.size() == 2);
1600 const qreal angle = parameters.value(1).toReal();
1601 if (hasRotationCenter) {
1602 const QPointF center = parameters.value(0).value<QPointF>();
1603 generateAnimatedPropertySetter(propertyTargetName,
1604 QStringLiteral(
"origin"),
1605 QVector3D(center.x(), center.y(), 0.0),
1610 generateAnimatedPropertySetter(propertyTargetName,
1611 QStringLiteral(
"angle"),
1618 case QTransform::TxShear:
1620 const QPointF skew = parameters.first().value<QPointF>();
1622 generateAnimatedPropertySetter(propertyTargetName,
1623 QStringLiteral(
"xAngle"),
1629 generateAnimatedPropertySetter(propertyTargetName,
1630 QStringLiteral(
"yAngle"),
1645 previousTime = time;
1646 previousParameters = parameters;
1658 if (firstAnimation.repeatCount >= 0) {
1659 stream() <<
"ScriptAction {";
1662 stream() <<
"script: {";
1666 stream() << targetName <<
"_transform_base_group.deactivate("
1667 << targetName <<
"_transform_group_" << groupIndex <<
")";
1668 }
else if (!replace) {
1669 stream() << targetName <<
"_transform_base_group.deactivateOverride("
1670 << targetName <<
"_transform_group_" << groupIndex <<
")";
1688void QQuickQmlGenerator::generateTransformTimeline(
const QString &targetName,
const NodeInfo &info)
1690 stream() <<
"Timeline {";
1692 stream() <<
"currentFrame: " << info.transform.timelineReferenceId() <<
".frameCounter";
1693 stream() <<
"enabled: true";
1695 const int groupIndex = 0;
1696 for (
int i = 0; i < info.transform.animationCount(); ++i) {
1697 const QQuickAnimatedProperty::PropertyAnimation &animation = info.transform.animation(i);
1698 if (animation.isConstant())
1700 if (info.transform.animationGroupCount() > 1
1701 || animation.repeatCount != 1 || animation.startOffset
1702 || animation.flags != QQuickAnimatedProperty::PropertyAnimation::FreezeAtEnd) {
1703 qCWarning(lcQuickVectorImage) <<
"Feature not implemented in timeline xf animation mode, for"
1704 << targetName <<
"subtype" << animation.subtype;
1707 bool hasRotationCenter =
false;
1708 if (animation.subtype == QTransform::TxRotate) {
1709 for (
auto it = animation.frames.constBegin(); it != animation.frames.constEnd(); ++it) {
1710 const QPointF center = it->value<QVariantList>().value(0).value<QPointF>();
1711 if (!center.isNull()) {
1712 hasRotationCenter =
true;
1718 auto pointFxExtractor = [](
const QVariant &value) {
return value.toPointF().x(); };
1719 auto pointFyExtractor = [](
const QVariant &value) {
return value.toPointF().y(); };
1720 auto realExtractor = [](
const QVariant &value) {
return value.toReal(); };
1721 auto pointFtoVector3dExtractor = [](
const QVariant &v) {
return QVector3D(v.toPointF()); };
1723 const QString propertyTargetName = targetName
1724 + QStringLiteral(
"_transform_")
1725 + QString::number(groupIndex)
1726 + QStringLiteral(
"_")
1727 + QString::number(i);
1729 switch (animation.subtype) {
1730 case QTransform::TxTranslate:
1731 generateTimelinePropertySetter(propertyTargetName,
"x"_L1, animation, pointFxExtractor);
1732 generateTimelinePropertySetter(propertyTargetName,
"y"_L1, animation, pointFyExtractor);
1734 case QTransform::TxScale:
1735 generateTimelinePropertySetter(propertyTargetName,
"xScale"_L1, animation, pointFxExtractor);
1736 generateTimelinePropertySetter(propertyTargetName,
"yScale"_L1, animation, pointFyExtractor);
1738 case QTransform::TxRotate:
1739 if (hasRotationCenter)
1740 generateTimelinePropertySetter(propertyTargetName,
"origin"_L1, animation, pointFtoVector3dExtractor);
1741 generateTimelinePropertySetter(propertyTargetName,
"angle"_L1, animation, realExtractor, 1);
1743 case QTransform::TxShear:
1744 generateTimelinePropertySetter(propertyTargetName,
"xAngle"_L1, animation, pointFxExtractor);
1745 generateTimelinePropertySetter(propertyTargetName,
"yAngle"_L1, animation, pointFyExtractor);
1754bool QQuickQmlGenerator::generateStructureNode(
const StructureNodeInfo &info)
1756 if (Q_UNLIKELY(errorState() || !isNodeVisible(info)))
1759 const bool isPathContainer = !info.forceSeparatePaths && info.isPathContainer;
1760 if (info.stage == StructureNodeStage::Start) {
1761 if (!info.clipBox.isEmpty()) {
1762 stream() <<
"Item { // Clip";
1765 stream() <<
"width: " << info.clipBox.width();
1766 stream() <<
"height: " << info.clipBox.height();
1767 stream() <<
"clip: true";
1770 if (isPathContainer) {
1771 generatePathContainer(info);
1772 }
else if (!info.customItemType.isEmpty()) {
1773 stream() << info.customItemType <<
" {";
1775 stream() <<
"Item { // Structure node";
1779 generateTimelineFields(info);
1781 if (!info.viewBox.isEmpty()) {
1782 stream() <<
"transform: [";
1784 bool translate = !qFuzzyIsNull(info.viewBox.x()) || !qFuzzyIsNull(info.viewBox.y());
1786 stream() <<
"Translate { x: " << -info.viewBox.x() <<
"; y: " << -info.viewBox.y() <<
" },";
1787 stream() <<
"Scale { xScale: width / " << info.viewBox.width() <<
"; yScale: height / " << info.viewBox.height() <<
" }";
1792 generateNodeBase(info);
1794 generateNodeEnd(info);
1795 if (isPathContainer)
1796 m_inShapeItemLevel--;
1798 if (!info.clipBox.isEmpty()) {
1807bool QQuickQmlGenerator::generateMaskNode(
const MaskNodeInfo &info)
1809 if (Q_UNLIKELY(errorState()))
1813 if (info.stage == StructureNodeStage::Start) {
1814 if (info.isMaskContentRelativeCoordinates) {
1815 m_contentRelativeMasks.insert(info.id);
1816 stream() <<
"Item {";
1818 stream() <<
"transform: Matrix4x4 {";
1820 stream() <<
"matrix: Qt.matrix4x4(";
1822 stream() << info.id <<
".contentWidth, 0, 0, " << info.id <<
".contentX,";
1823 stream() <<
"0, " << info.id <<
".contentHeight, 0, " << info.id <<
".contentY,";
1824 stream() <<
"0, 0, 1, 0,";
1825 stream() <<
"0, 0, 0, 1)";
1833 if (info.stage == StructureNodeStage::End) {
1834 if (info.isMaskContentRelativeCoordinates) {
1840 startDefsSuffixBlock();
1841 stream() <<
"Loader {";
1844 stream() <<
"id: " << info.id;
1845 if (!info.keepMaskVisible)
1846 stream() <<
"visible: false";
1847 stream() <<
"sourceComponent: " << info.id <<
"_container";
1848 stream() <<
"width: item !== null ? item.originalBounds.width : 0";
1849 stream() <<
"height: item !== null ? item.originalBounds.height : 0";
1851 if (info.boundsReferenceId.isEmpty()) {
1852 stream() <<
"property real maskX: " << info.maskRect.left();
1853 stream() <<
"property real maskY: " << info.maskRect.top();
1854 stream() <<
"property real maskWidth: " << info.maskRect.width();
1855 stream() <<
"property real maskHeight: " << info.maskRect.height();
1858 if (info.isMaskContentRelativeCoordinates) {
1859 stream() <<
"property real contentX: 0";
1860 stream() <<
"property real contentY: 0";
1861 stream() <<
"property real contentWidth: 1";
1862 stream() <<
"property real contentHeight: 1";
1865 stream() <<
"function maskRect(otherX, otherY, otherWidth, otherHeight) {";
1868 stream() <<
"return ";
1869 if (!info.boundsReferenceId.isEmpty()) {
1870 stream(SameLine) << info.boundsReferenceId <<
".originalBounds";
1871 }
else if (info.isMaskRectRelativeCoordinates) {
1874 << info.id <<
".maskX * otherWidth + otherX,"
1875 << info.id <<
".maskY * otherHeight + otherY,"
1876 << info.id <<
".maskWidth * otherWidth,"
1877 << info.id <<
".maskHeight * otherHeight)";
1881 << info.id <<
".maskX, "
1882 << info.id <<
".maskY, "
1883 << info.id <<
".maskWidth, "
1884 << info.id <<
".maskHeight)";
1893 endDefsSuffixBlock();
1899void QQuickQmlGenerator::generateFilterNode(
const FilterNodeInfo &info)
1901 if (Q_UNLIKELY(errorState()))
1904 stream() <<
"Item {";
1907 generateNodeBase(info);
1909 stream() <<
"property real originalWidth: filterSourceItem.sourceItem.originalBounds.width";
1910 stream() <<
"property real originalHeight: filterSourceItem.sourceItem.originalBounds.height";
1911 stream() <<
"property rect filterRect: " << info.id <<
"_filterParameters"
1912 <<
".adaptToFilterRect(0, 0, originalWidth, originalHeight)";
1914 for (qsizetype i = 0; i < info.steps.size();)
1915 i = generateFilterStep(info, i);
1918 startDefsSuffixBlock();
1919 stream() <<
"QtObject {";
1922 stream() <<
"id: " << info.id <<
"_filterParameters";
1923 stream() <<
"property int wrapMode: ";
1924 if (info.wrapMode == QSGTexture::Repeat)
1925 stream(SameLine) <<
"ShaderEffectSource.Repeat";
1927 stream(SameLine) <<
"ShaderEffectSource.ClampToEdge";
1929 stream() <<
"property rect filterRect: Qt.rect("
1930 << info.filterRect.x() <<
", "
1931 << info.filterRect.y() <<
", "
1932 << info.filterRect.width() <<
", "
1933 << info.filterRect.height() <<
")";
1935 stream() <<
"function adaptToFilterRect(sx, sy, sw, sh) {";
1938 if (info.csFilterRect == FilterNodeInfo::CoordinateSystem::Absolute) {
1939 stream() <<
"return Qt.rect(filterRect.x, filterRect.y, filterRect.width, filterRect.height)";
1941 stream() <<
"return Qt.rect(sx + sw * filterRect.x, sy + sh * filterRect.y, sw * filterRect.width, sh * filterRect.height)";
1949 endDefsSuffixBlock();
1951 generateNodeEnd(info);
1954qsizetype QQuickQmlGenerator::generateFilterStep(
const FilterNodeInfo &info,
1955 qsizetype stepIndex)
1957 const FilterNodeInfo::FilterStep &step = info.steps.at(stepIndex);
1958 const QString primitiveId = info.id + QStringLiteral(
"_primitive") + QString::number(stepIndex);
1962 QString inputId = step.input1 != FilterNodeInfo::FilterInput::SourceColor
1964 : QStringLiteral(
"filterSourceItem");
1966 bool isComposite =
false;
1967 switch (step.filterType) {
1968 case FilterNodeInfo::Type::Merge:
1970 const int maxNodeCount = 8;
1973 QList<std::pair<FilterNodeInfo::FilterInput, QString> > inputs;
1974 for (; stepIndex < info.steps.size(); ++stepIndex) {
1975 const FilterNodeInfo::FilterStep &nodeStep = info.steps.at(stepIndex);
1976 if (nodeStep.filterType != FilterNodeInfo::Type::MergeNode)
1979 inputs.emplace_back(nodeStep.input1, nodeStep.namedInput1);
1982 if (inputs.size() > maxNodeCount) {
1983 qCWarning(lcQuickVectorImage) <<
"Maximum of" << maxNodeCount
1984 <<
"nodes exceeded in merge effect.";
1987 if (inputs.isEmpty()) {
1988 qCWarning(lcQuickVectorImage) <<
"Merge effect requires at least one node.";
1992 stream() <<
"ShaderEffect {";
1995 stream() <<
"id: " << primitiveId;
1996 stream() <<
"visible: false";
1998 stream() <<
"fragmentShader: \"qrc:/qt-project.org/quickvectorimage/helpers/shaders_ng/femerge.frag.qsb\"";
1999 stream() <<
"width: source1.width";
2000 stream() <<
"height: source1.height";
2001 stream() <<
"property int sourceCount: " << std::min(qsizetype(8), inputs.size());
2003 for (
int i = 0; i < maxNodeCount; ++i) {
2004 auto input = i < inputs.size()
2006 : std::pair(FilterNodeInfo::FilterInput::None, u"null"_s);
2008 QString inputId = input.first != FilterNodeInfo::FilterInput::SourceColor
2010 : QStringLiteral(
"filterSourceItem");
2012 stream() <<
"property var source" << (i + 1) <<
": " << inputId;
2020 case FilterNodeInfo::Type::CompositeOver:
2021 case FilterNodeInfo::Type::CompositeOut:
2022 case FilterNodeInfo::Type::CompositeIn:
2023 case FilterNodeInfo::Type::CompositeXor:
2024 case FilterNodeInfo::Type::CompositeAtop:
2025 case FilterNodeInfo::Type::CompositeArithmetic:
2026 case FilterNodeInfo::Type::CompositeLighter:
2030 case FilterNodeInfo::Type::BlendNormal:
2031 case FilterNodeInfo::Type::BlendMultiply:
2032 case FilterNodeInfo::Type::BlendScreen:
2033 case FilterNodeInfo::Type::BlendDarken:
2034 case FilterNodeInfo::Type::BlendLighten:
2036 stream() <<
"ShaderEffect {";
2039 QString input2Id = step.input2 != FilterNodeInfo::FilterInput::SourceColor
2041 : QStringLiteral(
"filterSourceItem");
2043 stream() <<
"id: " << primitiveId;
2044 stream() <<
"visible: false";
2047 switch (step.filterType) {
2048 case FilterNodeInfo::Type::CompositeOver:
2049 shader = QStringLiteral(
"fecompositeover");
2051 case FilterNodeInfo::Type::CompositeOut:
2052 shader = QStringLiteral(
"fecompositeout");
2054 case FilterNodeInfo::Type::CompositeIn:
2055 shader = QStringLiteral(
"fecompositein");
2057 case FilterNodeInfo::Type::CompositeXor:
2058 shader = QStringLiteral(
"fecompositexor");
2060 case FilterNodeInfo::Type::CompositeAtop:
2061 shader = QStringLiteral(
"fecompositeatop");
2063 case FilterNodeInfo::Type::CompositeArithmetic:
2064 shader = QStringLiteral(
"fecompositearithmetic");
2066 case FilterNodeInfo::Type::CompositeLighter:
2067 shader = QStringLiteral(
"fecompositelighter");
2069 case FilterNodeInfo::Type::BlendNormal:
2070 shader = QStringLiteral(
"feblendnormal");
2072 case FilterNodeInfo::Type::BlendMultiply:
2073 shader = QStringLiteral(
"feblendmultiply");
2075 case FilterNodeInfo::Type::BlendScreen:
2076 shader = QStringLiteral(
"feblendscreen");
2078 case FilterNodeInfo::Type::BlendDarken:
2079 shader = QStringLiteral(
"feblenddarken");
2081 case FilterNodeInfo::Type::BlendLighten:
2082 shader = QStringLiteral(
"feblendlighten");
2088 stream() <<
"fragmentShader: \"qrc:/qt-project.org/quickvectorimage/helpers/shaders_ng/"
2089 << shader <<
".frag.qsb\"";
2090 stream() <<
"property var source: " << inputId;
2091 stream() <<
"property var source2: " << input2Id;
2092 stream() <<
"width: source.width";
2093 stream() <<
"height: source.height";
2096 QVector4D k = step.filterParameter.value<QVector4D>();
2097 stream() <<
"property var k: Qt.vector4d("
2110 case FilterNodeInfo::Type::Flood:
2112 stream() <<
"Rectangle {";
2115 stream() <<
"id: " << primitiveId;
2116 stream() <<
"visible: false";
2118 stream() <<
"width: " << inputId <<
".width";
2119 stream() <<
"height: " << inputId <<
".height";
2121 QColor floodColor = step.filterParameter.value<QColor>();
2122 stream() <<
"color: \"" << floodColor.name(QColor::HexArgb) <<
"\"";
2129 case FilterNodeInfo::Type::ColorMatrix:
2131 stream() <<
"ShaderEffect {";
2134 stream() <<
"id: " << primitiveId;
2135 stream() <<
"visible: false";
2137 stream() <<
"fragmentShader: \"qrc:/qt-project.org/quickvectorimage/helpers/shaders_ng/fecolormatrix.frag.qsb\"";
2138 stream() <<
"property var source: " << inputId;
2139 stream() <<
"width: source.width";
2140 stream() <<
"height: source.height";
2142 QGenericMatrix<5, 5, qreal> matrix = step.filterParameter.value<QGenericMatrix<5, 5, qreal> >();
2143 for (
int row = 0; row < 4; ++row) {
2146 for (
int col = 0; col < 5; ++col)
2147 stream() <<
"property real m_" << row <<
"_" << col <<
": " << matrix(col, row);
2156 case FilterNodeInfo::Type::Offset:
2158 stream() <<
"ShaderEffectSource {";
2161 stream() <<
"id: " << primitiveId;
2162 stream() <<
"visible: false";
2163 stream() <<
"sourceItem: " << inputId;
2164 stream() <<
"width: sourceItem.width + offset.x";
2165 stream() <<
"height: sourceItem.height + offset.y";
2167 QVector2D offset = step.filterParameter.value<QVector2D>();
2168 stream() <<
"property vector2d offset: Qt.vector2d(";
2169 if (step.csFilterParameter == FilterNodeInfo::CoordinateSystem::Absolute)
2170 stream(SameLine) << offset.x() <<
" / width, " << offset.y() <<
" / height)";
2172 stream(SameLine) << offset.x() <<
", " << offset.y() <<
")";
2174 stream() <<
"sourceRect: Qt.rect(-offset.x, -offset.y, width, height)";
2176 stream() <<
"ItemSpy {";
2178 stream() <<
"id: " << primitiveId <<
"_offset_itemspy";
2179 stream() <<
"anchors.fill: parent";
2183 stream() <<
"textureSize: " << primitiveId <<
"_offset_itemspy.requiredTextureSize";
2192 case FilterNodeInfo::Type::GaussianBlur:
2195 stream() <<
"MultiEffect {";
2198 stream() <<
"id: " << primitiveId;
2199 stream() <<
"visible: false";
2201 stream() <<
"source: " << inputId;
2202 stream() <<
"blurEnabled: true";
2203 stream() <<
"width: source.width";
2204 stream() <<
"height: source.height";
2206 const qreal maxDeviation(12.0);
2207 const qreal deviation = step.filterParameter.toReal();
2208 if (step.csFilterParameter == FilterNodeInfo::CoordinateSystem::Relative)
2209 stream() <<
"blur: Math.min(1.0, " << deviation <<
" * filterSourceItem.width / " << maxDeviation <<
")";
2211 stream() <<
"blur: " << std::min(qreal(1.0), deviation / maxDeviation);
2212 stream() <<
"blurMax: 64";
2220 qCWarning(lcQuickVectorImage) <<
"Unhandled filter type: " <<
int(step.filterType);
2222 stream() <<
"Item { id: " << primitiveId <<
" }";
2227 stream() <<
"ShaderEffectSource {";
2230 stream() <<
"id: " << step.outputName;
2231 if (stepIndex < info.steps.size())
2232 stream() <<
"visible: false";
2234 qreal x1, x2, y1, y2;
2235 step.filterPrimitiveRect.getCoords(&x1, &y1, &x2, &y2);
2236 if (step.csFilterParameter == FilterNodeInfo::CoordinateSystem::Absolute) {
2237 stream() <<
"property real fpx1: " << x1;
2238 stream() <<
"property real fpy1: " << y1;
2239 stream() <<
"property real fpx2: " << x2;
2240 stream() <<
"property real fpy2: " << y2;
2241 }
else if (step.csFilterParameter == FilterNodeInfo::CoordinateSystem::Relative) {
2245 stream() <<
"property real fpx1: " << x1 <<
" * filterSourceItem.sourceItem.originalBounds.width";
2246 stream() <<
"property real fpy1: " << y1 <<
" * filterSourceItem.sourceItem.originalBounds.height";
2247 stream() <<
"property real fpx2: " << x2 <<
" * filterSourceItem.sourceItem.originalBounds.width";
2248 stream() <<
"property real fpy2: " << y2 <<
" * filterSourceItem.sourceItem.originalBounds.height";
2250 stream() <<
"property real fpx1: parent.filterRect.x";
2251 stream() <<
"property real fpy1: parent.filterRect.y";
2252 stream() <<
"property real fpx2: parent.filterRect.x + parent.filterRect.width";
2253 stream() <<
"property real fpy2: parent.filterRect.y + parent.filterRect.height";
2256 stream() <<
"sourceItem: " << primitiveId;
2257 stream() <<
"sourceRect: Qt.rect(fpx1 - parent.filterRect.x, fpy1 - parent.filterRect.y, width, height)";
2259 stream() <<
"x: fpx1";
2260 stream() <<
"y: fpy1";
2261 stream() <<
"width: " <<
"fpx2 - fpx1";
2262 stream() <<
"height: " <<
"fpy2 - fpy1";
2264 stream() <<
"ItemSpy {";
2266 stream() <<
"id: " << primitiveId <<
"_itemspy";
2267 stream() <<
"anchors.fill: parent";
2271 stream() <<
"textureSize: " << primitiveId <<
"_itemspy.requiredTextureSize";
2279void QQuickQmlGenerator::generateTimelineFields(
const StructureNodeInfo &info)
2281 if (usingTimelineAnimation() && info.timelineInfo) {
2282 QString frameCounterRef = info.timelineInfo->frameCounterReference
2283 + QStringLiteral(
".frameCounter");
2285 if (info.timelineInfo->generateVisibility) {
2286 stream() <<
"visible: " << frameCounterRef <<
" >= " << info.timelineInfo->startFrame
2287 <<
" && " << frameCounterRef <<
" < " << info.timelineInfo->endFrame;
2290 if (info.timelineInfo->generateFrameCounter) {
2291 stream() <<
"property real frameCounter: ";
2292 if (info.timelineInfo->frameCounterMapper.isAnimated()) {
2294 stream(SameLine) <<
"0";
2295 generatePropertyTimeline(info.timelineInfo->frameCounterMapper, info.id,
"frameCounter"_L1);
2297 const auto offset = info.timelineInfo->frameCounterOffset;
2298 const auto multiplier = info.timelineInfo->frameCounterMultiplier;
2299 const bool needsParens = (offset && multiplier);
2301 stream(SameLine) <<
"(";
2302 stream(SameLine) << frameCounterRef;
2304 stream(SameLine) << (offset > 0 ?
" + " :
" - ") << qAbs(offset);
2306 stream(SameLine) <<
")";
2308 stream(SameLine) <<
" * " << multiplier;
2314bool QQuickQmlGenerator::generatePatternNode(
const PatternNodeInfo &info)
2316 if (info.stage == StructureNodeStage::Start) {
2319 startDefsSuffixBlock();
2320 stream() <<
"Loader {";
2323 stream() <<
"id: " << info.id;
2324 stream() <<
"sourceComponent: " << info.id <<
"_container";
2325 stream() <<
"width: item !== null ? item.originalBounds.width : 0";
2326 stream() <<
"height: item !== null ? item.originalBounds.height : 0";
2327 stream() <<
"visible: false";
2328 stream() <<
"function sourceRect(targetWidth, targetHeight) {";
2331 stream() <<
"return Qt.rect(0, 0, ";
2332 if (!info.isPatternRectRelativeCoordinates) {
2333 stream(SameLine) << info.patternRect.width() <<
", "
2334 << info.patternRect.height();
2336 stream(SameLine) << info.patternRect.width() <<
" * targetWidth, "
2337 << info.patternRect.height() <<
" * targetHeight";
2339 stream(SameLine) <<
")";
2343 stream() <<
"function sourceOffset(targetWidth, targetHeight) {";
2346 stream() <<
"return Qt.vector3d(";
2347 if (!info.isPatternRectRelativeCoordinates) {
2348 stream(SameLine) << info.patternRect.x() <<
", "
2349 << info.patternRect.y() <<
", ";
2351 stream(SameLine) << info.patternRect.x() <<
" * targetWidth, "
2352 << info.patternRect.y() <<
" * targetHeight, ";
2354 stream(SameLine) <<
"0.0)";
2362 endDefsSuffixBlock();
2368void QQuickQmlGenerator::generateDefsInstantiationNode(
const StructureNodeInfo &info)
2370 if (Q_UNLIKELY(errorState()))
2373 if (info.stage == StructureNodeStage::Start) {
2374 stream() <<
"Loader {";
2377 stream() <<
"sourceComponent: " << info.defsId <<
"_container";
2378 generateNodeBase(info);
2379 generateTimelineFields(info);
2386bool QQuickQmlGenerator::generateMarkerNode(
const MarkerNodeInfo &info)
2388 if (info.stage == StructureNodeStage::Start) {
2389 startDefsSuffixBlock();
2390 stream() <<
"QtObject {";
2393 stream() <<
"id: " << info.id <<
"_markerParameters";
2395 stream() <<
"property bool startReversed: ";
2396 if (info.orientation == MarkerNodeInfo::Orientation::AutoStartReverse)
2397 stream(SameLine) <<
"true";
2399 stream(SameLine) <<
"false";
2401 stream() <<
"function autoAngle(adaptedAngle) {";
2403 if (info.orientation == MarkerNodeInfo::Orientation::Value)
2404 stream() <<
"return " << info.angle;
2406 stream() <<
"return adaptedAngle";
2412 endDefsSuffixBlock();
2414 if (!info.clipBox.isEmpty()) {
2415 stream() <<
"Item {";
2418 stream() <<
"x: " << info.clipBox.x();
2419 if (info.markerUnits == MarkerNodeInfo::MarkerUnits::StrokeWidth)
2420 stream(SameLine) <<
" * strokeWidth";
2421 stream() <<
"y: " << info.clipBox.y();
2422 if (info.markerUnits == MarkerNodeInfo::MarkerUnits::StrokeWidth)
2423 stream(SameLine) <<
" * strokeWidth";
2424 stream() <<
"width: " << info.clipBox.width();
2425 if (info.markerUnits == MarkerNodeInfo::MarkerUnits::StrokeWidth)
2426 stream(SameLine) <<
" * strokeWidth";
2427 stream() <<
"height: " << info.clipBox.height();
2428 if (info.markerUnits == MarkerNodeInfo::MarkerUnits::StrokeWidth)
2429 stream(SameLine) <<
" * strokeWidth";
2430 stream() <<
"clip: true";
2433 stream() <<
"Item {";
2436 if (!info.clipBox.isEmpty()) {
2437 stream() <<
"x: " << -info.clipBox.x();
2438 if (info.markerUnits == MarkerNodeInfo::MarkerUnits::StrokeWidth)
2439 stream(SameLine) <<
" * strokeWidth";
2440 stream() <<
"y: " << -info.clipBox.y();
2441 if (info.markerUnits == MarkerNodeInfo::MarkerUnits::StrokeWidth)
2442 stream(SameLine) <<
" * strokeWidth";
2445 stream() <<
"id: " << info.id;
2447 stream() <<
"property real markerWidth: " << info.markerSize.width();
2448 if (info.markerUnits == MarkerNodeInfo::MarkerUnits::StrokeWidth)
2449 stream(SameLine) <<
" * strokeWidth";
2451 stream() <<
"property real markerHeight: " << info.markerSize.height();
2452 if (info.markerUnits == MarkerNodeInfo::MarkerUnits::StrokeWidth)
2453 stream(SameLine) <<
" * strokeWidth";
2455 stream() <<
"function calculateMarkerScale(w, h) {";
2458 stream() <<
"var scaleX = 1.0";
2459 stream() <<
"var scaleY = 1.0";
2460 stream() <<
"var offsetX = 0.0";
2461 stream() <<
"var offsetY = 0.0";
2462 if (info.viewBox.width() > 0)
2463 stream() <<
"if (w > 0) scaleX = w / " << info.viewBox.width();
2464 if (info.viewBox.height() > 0)
2465 stream() <<
"if (h > 0) scaleY = h / " << info.viewBox.height();
2467 if (info.preserveAspectRatio & MarkerNodeInfo::xyMask) {
2468 stream() <<
"if (scaleX != scaleY) {";
2471 if (info.preserveAspectRatio & MarkerNodeInfo::meet)
2472 stream() <<
"scaleX = scaleY = Math.min(scaleX, scaleY)";
2474 stream() <<
"scaleX = scaleY = Math.max(scaleX, scaleY)";
2476 QString overflowX = QStringLiteral(
"scaleX * %1 - w").arg(info.viewBox.width());
2477 QString overflowY = QStringLiteral(
"scaleY * %1 - h").arg(info.viewBox.height());
2479 const quint8 xRatio = info.preserveAspectRatio & MarkerNodeInfo::xMask;
2480 if (xRatio == MarkerNodeInfo::xMid)
2481 stream() <<
"offsetX -= " << overflowX <<
" / 2";
2482 else if (xRatio == MarkerNodeInfo::xMax)
2483 stream() <<
"offsetX -= " << overflowX;
2485 const quint8 yRatio = info.preserveAspectRatio & MarkerNodeInfo::yMask;
2486 if (yRatio == MarkerNodeInfo::yMid)
2487 stream() <<
"offsetY -= " << overflowY <<
" / 2";
2488 else if (yRatio == MarkerNodeInfo::yMax)
2489 stream() <<
"offsetY -= " << overflowY;
2495 stream() <<
"return Qt.vector4d("
2496 <<
"offsetX - " << info.anchorPoint.x() <<
" * scaleX, "
2497 <<
"offsetY - " << info.anchorPoint.y() <<
" * scaleY, "
2504 stream() <<
"property vector4d markerScale: calculateMarkerScale(markerWidth, markerHeight)";
2506 stream() <<
"transform: [";
2509 stream() <<
"Scale { xScale: " << info.id <<
".markerScale.z; yScale: " << info.id <<
".markerScale.w },";
2510 stream() <<
"Translate { x: " << info.id <<
".markerScale.x; y: " << info.id <<
".markerScale.y }";
2516 generateNodeEnd(info);
2518 if (!info.clipBox.isEmpty()) {
2527bool QQuickQmlGenerator::generateRootNode(
const StructureNodeInfo &info)
2529 if (Q_UNLIKELY(errorState()))
2532 const QStringList comments = m_commentString.split(u'\n');
2534 if (!isNodeVisible(info)) {
2537 if (comments.isEmpty()) {
2538 stream() <<
"// Generated from SVG";
2540 for (
const auto &comment : comments)
2541 stream() <<
"// " << comment;
2544 stream() <<
"import QtQuick";
2545 stream() <<
"import QtQuick.Shapes" << Qt::endl;
2546 stream() <<
"Item {";
2549 double w = info.size.width();
2550 double h = info.size.height();
2552 stream() <<
"implicitWidth: " << w;
2554 stream() <<
"implicitHeight: " << h;
2562 if (info.stage == StructureNodeStage::Start) {
2565 if (comments.isEmpty())
2566 stream() <<
"// Generated from SVG";
2568 for (
const auto &comment : comments)
2569 stream() <<
"// " << comment;
2571 stream() <<
"import QtQuick";
2572 stream() <<
"import QtQuick.VectorImage";
2573 stream() <<
"import QtQuick.VectorImage.Helpers";
2574 stream() <<
"import QtQuick.Shapes";
2575 stream() <<
"import QtQuick.Effects";
2576 if (usingTimelineAnimation())
2577 stream() <<
"import QtQuick.Timeline";
2579 for (
const auto &import : std::as_const(m_extraImports))
2580 stream() <<
"import " << import;
2582 stream() << Qt::endl <<
"Item {";
2585 double w = info.size.width();
2586 double h = info.size.height();
2588 stream() <<
"implicitWidth: " << w;
2590 stream() <<
"implicitHeight: " << h;
2592 if (Q_UNLIKELY(!isRuntimeGenerator())) {
2593 stream() <<
"component AnimationsInfo : QtObject";
2598 stream() <<
"property bool paused: false";
2599 stream() <<
"property int loops: 1";
2600 stream() <<
"signal restart()";
2602 if (Q_UNLIKELY(!isRuntimeGenerator())) {
2605 stream() <<
"property AnimationsInfo animations : AnimationsInfo {}";
2608 stream() <<
"Item {";
2610 stream() <<
"width: 1";
2611 stream() <<
"height: 1";
2613 stream() <<
"ItemSpy { id: __qt_toplevel_scale_itemspy; anchors.fill: parent }";
2618 if (!info.viewBox.isEmpty()) {
2619 stream() <<
"transform: [";
2621 bool translate = !qFuzzyIsNull(info.viewBox.x()) || !qFuzzyIsNull(info.viewBox.y());
2623 stream() <<
"Translate { x: " << -info.viewBox.x() <<
"; y: " << -info.viewBox.y() <<
" },";
2624 stream() <<
"Scale { xScale: width / " << info.viewBox.width() <<
"; yScale: height / " << info.viewBox.height() <<
" }";
2629 if (!info.forceSeparatePaths && info.isPathContainer) {
2630 m_topLevelIdString = QStringLiteral(
"__qt_toplevel");
2631 stream() <<
"id: " << m_topLevelIdString;
2633 generatePathContainer(info);
2636 generateNodeBase(info);
2638 m_topLevelIdString = generateNodeBase(info);
2639 if (m_topLevelIdString.isEmpty())
2640 qCWarning(lcQuickVectorImage) <<
"No ID specified for top level item";
2643 if (usingTimelineAnimation() && info.timelineInfo) {
2644 stream() <<
"property real startFrame: " << info.timelineInfo->startFrame;
2645 stream() <<
"property real endFrame: " << info.timelineInfo->endFrame;
2646 stream() <<
"property real frameRate: " << info.timelineInfo->frameRate;
2647 stream() <<
"property real frameCounter: " << info.timelineInfo->startFrame;
2648 if (!info.timelineInfo->markers.isEmpty()) {
2649 QStringList markerEntries;
2650 markerEntries.reserve(info.timelineInfo->markers.size());
2651 for (
const LottieMarkerInfo &marker : info.timelineInfo->markers) {
2652 markerEntries << u'"' + sanitizeString(marker.name) + u"\": ["
2653 + QString::number(marker.frame) + u", "
2654 + QString::number(marker.duration) + u']';
2656 stream() <<
"property var markers: ({" << markerEntries.join(
", "_L1) <<
"})";
2658 stream() <<
"NumberAnimation on frameCounter {";
2660 stream() <<
"objectName: \"_qt_frameCounterAnimation\"";
2661 stream() <<
"from: " << m_topLevelIdString <<
".startFrame";
2662 stream() <<
"to: " << m_topLevelIdString <<
".endFrame";
2663 stream() <<
"duration: " << processAnimationTime(1000) <<
" * Math.abs(to - from) / "
2664 <<
"Math.max(" << m_topLevelIdString <<
".frameRate, 1)";
2665 generateAnimationBindings();
2668 stream() <<
"visible: frameCounter >= " << info.timelineInfo->startFrame
2669 <<
" && frameCounter < " << info.timelineInfo->endFrame;
2672 if (m_inShapeItemLevel > 0) {
2673 m_inShapeItemLevel--;
2678 for (
const auto [coords, id] : m_easings.asKeyValueRange()) {
2679 stream() <<
"readonly property easingCurve " << id <<
": ({ type: Easing.BezierSpline, bezierCurve: [ ";
2680 for (
auto coord : coords)
2681 stream(SameLine) << coord <<
", ";
2682 stream(SameLine) <<
"1, 1 ] })";
2685 generateNodeEnd(info);
2692void QQuickQmlGenerator::startDefsSuffixBlock()
2694 int tmp = m_oldIndentLevels.top();
2695 m_oldIndentLevels.push(m_indentLevel);
2696 m_indentLevel = tmp;
2697 m_stream.setString(&m_defsSuffix);
2700void QQuickQmlGenerator::endDefsSuffixBlock()
2702 m_indentLevel = m_oldIndentLevels.pop();
2703 m_stream.setDevice(&m_result);
2706QStringView QQuickQmlGenerator::indent()
2708 int indentWidth = m_indentLevel * 4;
2709 if (indentWidth > m_indentString.size())
2710 m_indentString.fill(QLatin1Char(
' '), indentWidth * 2);
2711 return QStringView(m_indentString).first(indentWidth);
2714QTextStream &QQuickQmlGenerator::stream(
int flags)
2716 if (m_stream.device() ==
nullptr && m_stream.string() ==
nullptr)
2717 m_stream.setDevice(&m_result);
2718 else if (!(flags & StreamFlags::SameLine))
2719 m_stream << Qt::endl << indent();
2721 static qint64 maxBufferSize = qEnvironmentVariableIntegerValue(
"QT_QUICKVECTORIMAGE_MAX_BUFFER").value_or(64 << 20);
2722 if (m_stream.device()) {
2723 if (Q_UNLIKELY(!checkSanityLimit(m_stream.device()->size(), maxBufferSize,
"buffer size"_L1)))
2724 m_stream.device()->reset();
2726 if (Q_UNLIKELY(!checkSanityLimit(m_stream.string()->size(), maxBufferSize,
"buffer string size"_L1)))
2727 m_stream.string()->clear();
2733const char *QQuickQmlGenerator::shapeName()
const
2735 return m_shapeTypeName.isEmpty() ?
"Shape" : m_shapeTypeName.constData();
Combined button and popup list for selecting options.
static QString sanitizeString(const QString &input)