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>
25 s.replace(QLatin1Char(
'"'), QLatin1String(
"\\\""));
30QQuickAnimatedProperty::PropertyAnimation QQuickAnimatedProperty::PropertyAnimation::simplified()
const
32 QQuickAnimatedProperty::PropertyAnimation res = *
this;
33 int consecutiveEquals = 0;
34 int prevTimePoint = -1;
36 for (
const auto &[timePoint, value] : frames.asKeyValueRange()) {
37 if (value != prevValue) {
38 consecutiveEquals = 1;
40 }
else if (consecutiveEquals < 2) {
44 res.frames.remove(prevTimePoint);
45 res.easingPerFrame.remove(prevTimePoint);
47 prevTimePoint = timePoint;
53QQuickQmlGenerator::QQuickQmlGenerator(
const QString fileName, QQuickVectorImageGenerator::GeneratorFlags flags,
const QString &outFileName)
54 : QQuickGenerator(fileName, flags)
55 , outputFileName(outFileName)
57 m_result.open(QIODevice::ReadWrite);
58 m_oldIndentLevels.push(0);
61QQuickQmlGenerator::~QQuickQmlGenerator()
65bool QQuickQmlGenerator::save()
67 if (Q_UNLIKELY(errorState()))
71 if (!outputFileName.isEmpty()) {
72 QFileInfo fileInfo(outputFileName);
73 QDir dir(fileInfo.absolutePath());
74 if (!dir.exists() && !dir.mkpath(QStringLiteral(
"."))) {
75 qCWarning(lcQuickVectorImage) <<
"Failed to create path" << dir.absolutePath();
78 QFile outFile(outputFileName);
79 if (outFile.open(QIODevice::WriteOnly)) {
80 outFile.write(m_result.data());
83 qCWarning(lcQuickVectorImage) <<
"Failed to write to file" << outFile.fileName();
89 if (lcQuickVectorImage().isDebugEnabled())
90 qCDebug(lcQuickVectorImage).noquote() << m_result.data().left(300);
95void QQuickQmlGenerator::setShapeTypeName(
const QString &name)
97 m_shapeTypeName = name.toLatin1();
100QString QQuickQmlGenerator::shapeTypeName()
const
102 return QString::fromLatin1(m_shapeTypeName);
105void QQuickQmlGenerator::setCommentString(
const QString commentString)
107 m_commentString = commentString;
110QString QQuickQmlGenerator::commentString()
const
112 return m_commentString;
115QString QQuickQmlGenerator::generateNodeBase(
const NodeInfo &info,
const QString &idSuffix)
117 static qint64 maxNodes = qEnvironmentVariableIntegerValue(
"QT_QUICKVECTORIMAGE_MAX_NODES").value_or(10000);
118 if (Q_UNLIKELY(!checkSanityLimit(++m_nodeCounter, maxNodes,
"nodes"_L1)))
121 if (!info.nodeId.isEmpty())
122 stream() <<
"objectName: \"" << info.nodeId <<
"\"";
124 if (!info.id.isEmpty())
125 stream() <<
"id: " << info.id << idSuffix;
127 if (!info.bounds.isNull() || !info.boundsReferenceId.isEmpty()) {
128 stream() <<
"property var originalBounds: ";
129 if (!info.bounds.isNull()) {
130 stream(SameLine) <<
"Qt.rect(" << info.bounds.x() <<
", " << info.bounds.y() <<
", "
131 << info.bounds.width() <<
", " << info.bounds.height() <<
")";
133 stream(SameLine) << info.boundsReferenceId <<
".originalBounds";
135 stream() <<
"width: originalBounds.width";
136 stream() <<
"height: originalBounds.height";
139 stream() <<
"transformOrigin: Item.TopLeft";
141 if (info.filterId.isEmpty() && info.maskId.isEmpty()) {
142 if (!info.isDefaultOpacity)
143 stream() <<
"opacity: " << info.opacity.defaultValue().toReal();
144 generateItemAnimations(info.id, info);
150void QQuickQmlGenerator::generateNodeEnd(
const NodeInfo &info)
152 if (Q_UNLIKELY(errorState()))
156 generateShaderUse(info);
159void QQuickQmlGenerator::generateItemAnimations(
const QString &idString,
const NodeInfo &info)
161 const bool hasTransform = info.transform.isAnimated()
162 || !info.maskId.isEmpty()
163 || !info.filterId.isEmpty()
164 || !info.isDefaultTransform
165 || !info.transformReferenceId.isEmpty()
166 || info.motionPath.isAnimated();
169 stream() <<
"transform: TransformGroup {";
172 bool hasNonConstantTransform =
false;
173 int earliestOverrideGroup = -1;
175 if (!idString.isEmpty()) {
176 stream() <<
"id: " << idString <<
"_transform_base_group";
178 if (!info.maskId.isEmpty() || !info.filterId.isEmpty())
179 stream() <<
"Translate { x: " << idString <<
".sourceX; y: " << idString <<
".sourceY }";
181 if (info.transform.isAnimated()) {
182 for (
int groupIndex = 0; groupIndex < info.transform.animationGroupCount(); ++groupIndex) {
183 stream() <<
"TransformGroup {";
186 if (!idString.isEmpty())
187 stream() <<
"id: " << idString <<
"_transform_group_" << groupIndex;
189 int animationStart = info.transform.animationGroup(groupIndex);
190 int nextAnimationStart = groupIndex + 1 < info.transform.animationGroupCount()
191 ? info.transform.animationGroup(groupIndex + 1)
192 : info.transform.animationCount();
194 const QQuickAnimatedProperty::PropertyAnimation &firstAnimation = info.transform.animation(animationStart);
195 const bool replace = firstAnimation.flags & QQuickAnimatedProperty::PropertyAnimation::ReplacePreviousAnimations;
196 if (replace && earliestOverrideGroup < 0)
197 earliestOverrideGroup = groupIndex;
199 for (
int i = nextAnimationStart - 1; i >= animationStart; --i) {
200 const QQuickAnimatedProperty::PropertyAnimation &animation = info.transform.animation(i);
201 if (animation.frames.isEmpty())
204 const QVariantList ¶meters = animation.frames.first().value<QVariantList>();
205 switch (animation.subtype) {
206 case QTransform::TxTranslate:
207 if (animation.isConstant()) {
208 const QPointF translation = parameters.value(0).value<QPointF>();
209 if (!translation.isNull())
210 stream() <<
"Translate { x: " << translation.x() <<
"; y: " << translation.y() <<
" }";
212 hasNonConstantTransform =
true;
213 stream() <<
"Translate { id: " << idString <<
"_transform_" << groupIndex <<
"_" << i <<
" }";
216 case QTransform::TxScale:
217 if (animation.isConstant()) {
218 const QPointF scale = parameters.value(0).value<QPointF>();
219 if (scale != QPointF(1, 1))
220 stream() <<
"Scale { xScale: " << scale.x() <<
"; yScale: " << scale.y() <<
" }";
222 hasNonConstantTransform =
true;
223 stream() <<
"Scale { id: " << idString <<
"_transform_" << groupIndex <<
"_" << i <<
"}";
226 case QTransform::TxRotate:
227 if (animation.isConstant()) {
228 const QPointF center = parameters.value(0).value<QPointF>();
229 const qreal angle = parameters.value(1).toReal();
230 if (!qFuzzyIsNull(angle))
231 stream() <<
"Rotation { angle: " << angle <<
"; origin.x: " << center.x() <<
"; origin.y: " << center.y() <<
" }";
233 hasNonConstantTransform =
true;
234 stream() <<
"Rotation { id: " << idString <<
"_transform_" << groupIndex <<
"_" << i <<
" }";
237 case QTransform::TxShear:
238 if (animation.isConstant()) {
239 const QPointF skew = parameters.value(0).value<QPointF>();
241 stream() <<
"Shear { xAngle: " << skew.x() <<
"; yAngle: " << skew.y() <<
" }";
243 hasNonConstantTransform =
true;
244 stream() <<
"Shear { id: " << idString <<
"_transform_" << groupIndex <<
"_" << i <<
" }";
257 if (info.motionPath.isAnimated()) {
258 QVariantList defaultProps = info.motionPath.defaultValue().value<QVariantList>();
259 const bool adaptAngle = defaultProps.value(1).toBool();
260 const qreal baseRotation = defaultProps.value(2).toReal();
261 QString interpolatorId = idString + QStringLiteral(
"_motion_interpolator");
262 if (adaptAngle || !qFuzzyIsNull(baseRotation)) {
263 stream() <<
"Rotation {";
267 stream() <<
"angle: " << interpolatorId <<
".angle";
268 if (!qFuzzyIsNull(baseRotation))
269 stream(SameLine) <<
" + " << baseRotation;
271 stream() <<
"angle: " << baseRotation;
278 stream() <<
"Translate {";
281 stream() <<
"x: " << interpolatorId <<
".x";
282 stream() <<
"y: " << interpolatorId <<
".y";
289 if (!info.isDefaultTransform) {
290 QTransform xf = info.transform.defaultValue().value<QTransform>();
291 if (xf.type() <= QTransform::TxTranslate) {
292 stream() <<
"Translate { x: " << xf.dx() <<
"; y: " << xf.dy() <<
"}";
294 stream() <<
"Matrix4x4 { matrix: ";
295 generateTransform(xf);
296 stream(SameLine) <<
"}";
300 if (!info.transformReferenceId.isEmpty())
301 stream() <<
"Matrix4x4 { matrix: " << info.transformReferenceId <<
".transformMatrix }";
306 if (hasNonConstantTransform) {
307 generateAnimateTransform(idString, info);
308 }
else if (info.transform.isAnimated() && earliestOverrideGroup >= 0) {
311 stream() <<
"Component.onCompleted: {";
314 stream() << idString <<
"_transform_base_group.activateOverride("
315 << idString <<
"_transform_group_" << earliestOverrideGroup <<
")";
322 generateAnimateMotionPath(idString, info.motionPath);
324 generatePropertyAnimation(info.opacity, idString, QStringLiteral(
"opacity"));
327void QQuickQmlGenerator::generateShaderUse(
const NodeInfo &info)
329 const bool hasMask = !info.maskId.isEmpty();
330 const bool hasFilters = !info.filterId.isEmpty();
331 if (!hasMask && !hasFilters)
334 const QString effectId = hasFilters
335 ? info.filterId + QStringLiteral(
"_") + info.id + QStringLiteral(
"_effect")
338 QString animatedItemId;
340 stream() <<
"ShaderEffectSource {";
343 const QString seId = info.id + QStringLiteral(
"_se");
344 stream() <<
"id: " << seId;
346 stream() <<
"ItemSpy {";
348 stream() <<
"id: " << info.id <<
"_itemspy";
349 stream() <<
"anchors.fill: parent";
353 stream() <<
"hideSource: true";
354 stream() <<
"wrapMode: " << info.filterId <<
"_filterParameters.wrapMode";
355 stream() <<
"sourceItem: " << info.id;
356 stream() <<
"sourceRect: " << info.filterId
357 <<
"_filterParameters.adaptToFilterRect("
358 << info.id <<
".originalBounds.x, "
359 << info.id <<
".originalBounds.y, "
360 << info.id <<
".originalBounds.width, "
361 << info.id <<
".originalBounds.height)";
362 stream() <<
"textureSize: " << info.id <<
"_itemspy.requiredTextureSize";
363 stream() <<
"width: sourceRect.width";
364 stream() <<
"height: sourceRect.height";
365 stream() <<
"visible: false";
370 stream() <<
"Loader {";
373 animatedItemId = effectId;
374 stream() <<
"id: " << effectId;
376 stream() <<
"property var filterSourceItem: " << seId;
377 stream() <<
"sourceComponent: " << info.filterId <<
"_container";
378 stream() <<
"property real sourceX: " << info.id <<
".originalBounds.x";
379 stream() <<
"property real sourceY: " << info.id <<
".originalBounds.y";
380 stream() <<
"width: " << info.id <<
".originalBounds.width";
381 stream() <<
"height: " << info.id <<
".originalBounds.height";
391 stream() <<
"ShaderEffectSource {";
394 const QString maskId = info.maskId + QStringLiteral(
"_") + info.id + QStringLiteral(
"_mask");
395 stream() <<
"id: " << maskId;
396 stream() <<
"sourceItem: " << info.maskId;
397 stream() <<
"visible: false";
398 stream() <<
"hideSource: true";
400 stream() <<
"ItemSpy {";
402 stream() <<
"id: " << maskId <<
"_itemspy";
403 stream() <<
"anchors.fill: parent";
406 stream() <<
"textureSize: " << maskId <<
"_itemspy.requiredTextureSize";
408 stream() <<
"sourceRect: " << info.maskId <<
".maskRect("
409 << info.id <<
".originalBounds.x,"
410 << info.id <<
".originalBounds.y,"
411 << info.id <<
".originalBounds.width,"
412 << info.id <<
".originalBounds.height)";
414 stream() <<
"width: sourceRect.width";
415 stream() <<
"height: sourceRect.height";
421 stream() <<
"ShaderEffectSource {";
424 const QString seId = info.id + QStringLiteral(
"_masked_se");
425 stream() <<
"id: " << seId;
427 stream() <<
"ItemSpy {";
429 stream() <<
"id: " << info.id <<
"_masked_se_itemspy";
430 stream() <<
"anchors.fill: parent";
434 stream() <<
"hideSource: true";
436 stream() <<
"sourceItem: " << effectId;
438 stream() <<
"sourceItem: " << info.id;
439 stream() <<
"textureSize: " << info.id <<
"_masked_se_itemspy.requiredTextureSize";
441 stream() <<
"sourceRect: " << info.maskId <<
".maskRect("
442 << info.id <<
".originalBounds.x,"
443 << info.id <<
".originalBounds.y,"
444 << info.id <<
".originalBounds.width,"
445 << info.id <<
".originalBounds.height)";
447 stream() <<
"sourceRect: " << info.maskId <<
".maskRect(0, 0,"
448 << info.id <<
".originalBounds.width,"
449 << info.id <<
".originalBounds.height)";
451 stream() <<
"width: sourceRect.width";
452 stream() <<
"height: sourceRect.height";
453 stream() <<
"smooth: false";
454 stream() <<
"visible: false";
459 stream() <<
"ShaderEffect {";
462 const QString maskShaderId = maskId + QStringLiteral(
"_se");
463 animatedItemId = maskShaderId;
465 stream() <<
"id:" << maskShaderId;
467 stream() <<
"property real sourceX: " << maskId <<
".sourceRect.x";
468 stream() <<
"property real sourceY: " << maskId <<
".sourceRect.y";
469 stream() <<
"width: " << maskId <<
".sourceRect.width";
470 stream() <<
"height: " << maskId <<
".sourceRect.height";
472 stream() <<
"fragmentShader: \"qrc:/qt-project.org/quickvectorimage/helpers/shaders_ng/genericmask.frag.qsb\"";
473 stream() <<
"property var source: " << seId;
474 stream() <<
"property var maskSource: " << maskId;
475 stream() <<
"property bool isAlpha: " << (info.isMaskAlpha ?
"true" :
"false");
476 stream() <<
"property bool isInverted: " << (info.isMaskInverted ?
"true" :
"false");
479 if (!info.isDefaultOpacity)
480 stream() <<
"opacity: " << info.opacity.defaultValue().toReal();
482 generateItemAnimations(animatedItemId, info);
488bool QQuickQmlGenerator::generateDefsNode(
const StructureNodeInfo &info)
490 if (Q_UNLIKELY(errorState()))
493 if (info.stage == StructureNodeStage::Start) {
494 m_oldIndentLevels.push(m_indentLevel);
496 stream() <<
"Component {";
499 stream() <<
"id: " << info.id <<
"_container";
501 stream() <<
"Item {";
504 generateTimelineFields(info);
505 if (!info.transformReferenceChildId.isEmpty()) {
506 stream() <<
"property alias transformMatrix: "
507 << info.transformReferenceChildId <<
".transformMatrix";
510 generateNodeBase(info, QStringLiteral(
"_defs"));
512 generateNodeEnd(info);
517 stream() << m_defsSuffix;
518 m_defsSuffix.clear();
520 m_indentLevel = m_oldIndentLevels.pop();
526void QQuickQmlGenerator::generateImageNode(
const ImageNodeInfo &info)
528 if (Q_UNLIKELY(errorState() || !isNodeVisible(info)))
531 const QFileInfo outputFileInfo(outputFileName);
532 const QDir outputDir(outputFileInfo.absolutePath());
536 if (!m_retainFilePaths || info.externalFileReference.isEmpty()) {
537 filePath = m_assetFileDirectory;
538 if (filePath.isEmpty())
539 filePath = outputDir.absolutePath();
541 if (!filePath.isEmpty() && !filePath.endsWith(u'/'))
544 QDir fileDir(filePath);
545 if (!fileDir.exists()) {
546 if (!fileDir.mkpath(QStringLiteral(
".")))
547 qCWarning(lcQuickVectorImage) <<
"Failed to create image resource directory:" << filePath;
550 filePath += QStringLiteral(
"%1%2.png").arg(m_assetFilePrefix.isEmpty()
551 ? QStringLiteral(
"svg_asset_")
553 .arg(info.image.cacheKey());
555 if (!info.image.save(filePath))
556 qCWarning(lcQuickVectorImage) <<
"Unabled to save image resource" << filePath;
557 qCDebug(lcQuickVectorImage) <<
"Saving copy of IMAGE" << filePath;
559 filePath = info.externalFileReference;
562 const QFileInfo assetFileInfo(filePath);
564 stream() <<
"Image {";
567 generateNodeBase(info);
568 stream() <<
"x: " << info.rect.x();
569 stream() <<
"y: " << info.rect.y();
570 stream() <<
"width: " << info.rect.width();
571 stream() <<
"height: " << info.rect.height();
572 stream() <<
"source: \"" << m_urlPrefix << outputDir.relativeFilePath(assetFileInfo.absoluteFilePath()) <<
"\"";
573 generateNodeEnd(info);
576void QQuickQmlGenerator::generateMarkers(
const PathNodeInfo &info)
578 const QPainterPath path = info.path.defaultValue().value<QPainterPath>();
579 for (
int i = 0; i < path.elementCount(); ++i) {
580 const QPainterPath::Element element = path.elementAt(i);
585 auto getMeanAngle = [](QPointF p0, QPointF p1, QPointF p2) -> qreal {
586 QPointF t1 = p1 - p0;
587 QPointF t2 = p2 - p1;
588 qreal hyp1 = hypot(t1.x(), t1.y());
593 qreal hyp2 = hypot(t2.x(), t2.y());
598 QPointF tangent = t1 + t2;
599 return -atan2(tangent.y(), tangent.x()) / M_PI * 180.;
603 markerId = info.markerStartId;
604 angle = path.angleAtPercent(0.0);
605 }
else if (i == path.elementCount() - 1) {
606 markerId = info.markerEndId;
607 angle = path.angleAtPercent(1.0);
608 }
else if (path.elementAt(i + 1).type != QPainterPath::CurveToDataElement) {
609 markerId = info.markerMidId;
611 const QPainterPath::Element prevElement = path.elementAt(i - 1);
612 const QPainterPath::Element nextElement = path.elementAt(i + 1);
614 QPointF p1(prevElement.x, prevElement.y);
615 QPointF p2(element.x, element.y);
616 QPointF p3(nextElement.x, nextElement.y);
618 angle = getMeanAngle(p1, p2, p3);
621 if (!markerId.isEmpty()) {
622 stream() <<
"Loader {";
626 stream() <<
"sourceComponent: " << markerId <<
"_container";
627 stream() <<
"property real strokeWidth: " << info.strokeStyle.width;
628 stream() <<
"transform: [";
631 stream() <<
"Scale { "
632 <<
"xScale: " << markerId <<
"_markerParameters.startReversed ? -1 : 1; "
633 <<
"yScale: " << markerId <<
"_markerParameters.startReversed ? -1 : 1 },";
635 stream() <<
"Rotation { angle: " << markerId <<
"_markerParameters.autoAngle(" << -angle <<
") },";
636 stream() <<
"Translate { x: " << element.x <<
"; y: " << element.y <<
"}";
647void QQuickQmlGenerator::generatePath(
const PathNodeInfo &info,
const QRectF &overrideBoundingRect)
649 if (Q_UNLIKELY(errorState() || !isNodeVisible(info)))
652 if (m_inShapeItemLevel > 0) {
653 if (!info.isDefaultTransform)
654 qWarning() <<
"Skipped transform for node" << info.nodeId <<
"type" << info.typeName <<
"(this is not supposed to happen)";
655 optimizePaths(info, overrideBoundingRect);
657 m_inShapeItemLevel++;
658 stream() << shapeName() <<
" {";
661 generateNodeBase(info);
663 if (m_flags.testFlag(QQuickVectorImageGenerator::GeneratorFlag::CurveRenderer))
664 stream() <<
"preferredRendererType: Shape.CurveRenderer";
665 if (m_flags.testFlag(QQuickVectorImageGenerator::GeneratorFlag::AsyncShapes))
666 stream() <<
"asynchronous: true";
667 optimizePaths(info, overrideBoundingRect);
670 if (!info.markerStartId.isEmpty()
671 || !info.markerMidId.isEmpty()
672 || !info.markerEndId.isEmpty()) {
673 generateMarkers(info);
676 generateNodeEnd(info);
677 m_inShapeItemLevel--;
681void QQuickQmlGenerator::generateGradient(
const QGradient *grad,
682 const QString &propertyName,
683 const QRectF &coordinateConversion)
685 const QSizeF &scale = coordinateConversion.size();
686 const QPointF &translation = coordinateConversion.topLeft();
688 if (grad->type() == QGradient::LinearGradient) {
689 auto *linGrad =
static_cast<
const QLinearGradient *>(grad);
690 stream() << propertyName <<
": LinearGradient {";
693 QRectF gradRect(linGrad->start(), linGrad->finalStop());
695 stream() <<
"x1: " << (gradRect.left() * scale.width()) + translation.x();
696 stream() <<
"y1: " << (gradRect.top() * scale.height()) + translation.y();
697 stream() <<
"x2: " << (gradRect.right() * scale.width()) + translation.x();
698 stream() <<
"y2: " << (gradRect.bottom() * scale.height()) + translation.y();
699 for (
auto &stop : linGrad->stops())
700 stream() <<
"GradientStop { position: " << QString::number(stop.first,
'g', 7)
701 <<
"; color: \"" << stop.second.name(QColor::HexArgb) <<
"\" }";
702 }
else if (grad->type() == QGradient::RadialGradient) {
703 auto *radGrad =
static_cast<
const QRadialGradient*>(grad);
704 stream() << propertyName <<
": RadialGradient {";
707 stream() <<
"centerX: " << (radGrad->center().x() * scale.width()) + translation.x();
708 stream() <<
"centerY: " << (radGrad->center().y() * scale.height()) + translation.y();
709 stream() <<
"centerRadius: " << (radGrad->radius() * scale.width());
710 stream() <<
"focalX:" << (radGrad->focalPoint().x() * scale.width()) + translation.x();
711 stream() <<
"focalY:" << (radGrad->focalPoint().y() * scale.height()) + translation.y();
712 for (
auto &stop : radGrad->stops())
713 stream() <<
"GradientStop { position: " << QString::number(stop.first,
'g', 7)
714 <<
"; color: \"" << stop.second.name(QColor::HexArgb) <<
"\" }";
717 stream() <<
"spread: ShapeGradient.";
718 switch (grad->spread()) {
719 case QGradient::PadSpread:
720 stream(SameLine) <<
"PadSpread";
722 case QGradient::ReflectSpread:
723 stream(SameLine) <<
"ReflectSpread";
725 case QGradient::RepeatSpread:
726 stream(SameLine) <<
"RepeatSpread";
734void QQuickQmlGenerator::generateAnimationBindings()
737 if (Q_UNLIKELY(!isRuntimeGenerator()))
738 prefix = QStringLiteral(
".animations");
740 stream() <<
"loops: " << m_topLevelIdString << prefix <<
".loops";
741 stream() <<
"paused: " << m_topLevelIdString << prefix <<
".paused";
742 stream() <<
"running: true";
745 stream() <<
"onLoopsChanged: { if (running) { restart() } }";
748void QQuickQmlGenerator::generateEasing(
const QQuickAnimatedProperty::PropertyAnimation &animation,
749 int time,
int streamFlags)
751 if (animation.easingPerFrame.contains(time)) {
752 QBezier bezier = animation.easingPerFrame.value(time);
753 QPointF c1 = bezier.pt2();
754 QPointF c2 = bezier.pt3();
756 bool isLinear = (c1 == c1.transposed() && c2 == c2.transposed());
758 int nextIdx = m_easings.size();
759 QString &id = m_easings[{c1.x(), c1.y(), c2.x(), c2.y()}];
761 id = QString(QLatin1String(
"easing_%1")).arg(nextIdx, 2, 10, QLatin1Char(
'0'));
762 if (streamFlags & SameLine)
763 stream(streamFlags) <<
"; ";
764 stream(streamFlags) <<
"easing: " << m_topLevelIdString <<
"." << id;
771 static qreal multiplier = qreal(qEnvironmentVariable(
"QT_QUICKVECTORIMAGE_TIME_DILATION", QStringLiteral(
"1.0"))
773 return std::round(multiplier * time);
776void QQuickQmlGenerator::generatePropertyAnimation(
const QQuickAnimatedProperty &property,
777 const QString &targetName,
778 const QString &propertyName,
779 AnimationType animationType)
781 if (!property.isAnimated())
784 if (usingTimelineAnimation())
785 return generatePropertyTimeline(property, targetName, propertyName, animationType);
787 QString mainAnimationId = targetName
788 + QStringLiteral(
"_")
790 + QStringLiteral(
"_animation");
791 mainAnimationId.replace(QLatin1Char(
'.'), QLatin1Char(
'_'));
794 if (Q_UNLIKELY(!isRuntimeGenerator()))
795 prefix = QStringLiteral(
".animations");
797 stream() <<
"Connections { target: " << m_topLevelIdString << prefix <<
"; function onRestart() {" << mainAnimationId <<
".restart() } }";
799 stream() <<
"ParallelAnimation {";
802 stream() <<
"id: " << mainAnimationId;
804 generateAnimationBindings();
806 for (
int i = 0; i < property.animationCount(); ++i) {
807 const QQuickAnimatedProperty::PropertyAnimation &animation = property.animation(i);
809 stream() <<
"SequentialAnimation {";
812 const int startOffset = processAnimationTime(animation.startOffset);
814 stream() <<
"PauseAnimation { duration: " << startOffset <<
" }";
816 stream() <<
"SequentialAnimation {";
819 const int repeatCount = animation.repeatCount;
821 stream() <<
"loops: Animation.Infinite";
823 stream() <<
"loops: " << repeatCount;
825 int previousTime = 0;
826 QVariant previousValue;
827 for (
auto it = animation.frames.constBegin(); it != animation.frames.constEnd(); ++it) {
828 const int time = it.key();
829 const int frameTime = processAnimationTime(time - previousTime);
830 const QVariant &value = it.value();
832 if (previousValue.isValid() && previousValue == value) {
834 stream() <<
"PauseAnimation { duration: " << frameTime <<
" }";
835 }
else if (animationType == AnimationType::Auto && value.typeId() == QMetaType::Bool) {
838 stream() <<
"PauseAnimation { duration: " << frameTime <<
" }";
839 stream() <<
"ScriptAction {";
842 stream() <<
"script:" << targetName <<
"." << propertyName <<
" = " << value.toString();
847 generateAnimatedPropertySetter(targetName,
857 previousValue = value;
860 if (!(animation.flags & QQuickAnimatedProperty::PropertyAnimation::FreezeAtEnd)) {
861 stream() <<
"ScriptAction {";
863 stream() <<
"script: ";
865 switch (animationType) {
866 case AnimationType::Auto:
867 stream(SameLine) << targetName <<
"." << propertyName <<
" = ";
869 case AnimationType::ColorOpacity:
870 stream(SameLine) << targetName <<
"." << propertyName <<
".a = ";
874 QVariant value = property.defaultValue();
875 if (value.typeId() == QMetaType::QColor)
876 stream(SameLine) <<
"\"" << value.toString() <<
"\"";
878 stream(SameLine) << value.toReal();
895void QQuickQmlGenerator::generateTimelinePropertySetter(
896 const QString &targetName,
897 const QString &propertyName,
898 const QQuickAnimatedProperty::PropertyAnimation &animation,
899 std::function<QVariant(
const QVariant &)>
const& extractValue,
902 if (animation.repeatCount != 1 || animation.startOffset
903 || animation.flags != QQuickAnimatedProperty::PropertyAnimation::FreezeAtEnd) {
904 qCWarning(lcQuickVectorImage) <<
"Animation feature not implemented in timeline mode, for"
905 << targetName << propertyName;
908 stream() <<
"KeyframeGroup {";
910 stream() <<
"target: " << targetName;
911 stream() <<
"property: \"" << propertyName <<
"\"";
913 for (
const auto &[frame, rawValue] : animation.frames.asKeyValueRange()) {
915 if (rawValue.typeId() == QMetaType::QVariantList)
916 value = extractValue(rawValue.toList().value(valueIndex));
918 value = extractValue(rawValue);
920 stream() <<
"Keyframe { frame: " << frame <<
"; value: ";
921 if (value.typeId() == QMetaType::QVector3D) {
922 const QVector3D &v = value.value<QVector3D>();
923 stream(SameLine) <<
"Qt.vector3d(" << v.x() <<
", " << v.y() <<
", " << v.z() <<
")";
924 }
else if (value.typeId() == QMetaType::QColor) {
925 stream(SameLine) <<
"\"" << value.toString() <<
"\"";
927 stream(SameLine) << value.toReal();
929 generateEasing(animation, frame, SameLine);
930 stream(SameLine) <<
" }";
937void QQuickQmlGenerator::generatePropertyTimeline(
const QQuickAnimatedProperty &property,
938 const QString &targetName,
939 const QString &propertyName,
940 AnimationType animationType)
942 if (animationType == QQuickQmlGenerator::AnimationType::ColorOpacity) {
943 qCWarning(lcQuickVectorImage) <<
"ColorOpacity animation not available in timeline mode";
947 if (property.animationGroupCount() > 1 || property.animationCount() > 1) {
948 qCWarning(lcQuickVectorImage) <<
"Property feature not implemented in timeline mode, for"
949 << targetName << propertyName;
952 stream() <<
"Timeline {";
954 stream() <<
"currentFrame: " << property.timelineReferenceId() <<
".frameCounter";
955 stream() <<
"enabled: true";
957 auto extractor = [](
const QVariant &value) {
return value; };
958 generateTimelinePropertySetter(targetName, propertyName, property.animation(0), extractor);
964void QQuickQmlGenerator::generateTransform(
const QTransform &xf)
967 stream(SameLine) <<
"PlanarTransform.fromAffineMatrix("
968 << xf.m11() <<
", " << xf.m12() <<
", "
969 << xf.m21() <<
", " << xf.m22() <<
", "
970 << xf.dx() <<
", " << xf.dy() <<
")";
973 stream(SameLine) <<
"Qt.matrix4x4(";
975 const auto *data = m.data();
976 for (
int i = 0; i < 4; i++) {
977 stream() << data[i] <<
", " << data[i+4] <<
", " << data[i+8] <<
", " << data[i+12];
979 stream(SameLine) <<
", ";
981 stream(SameLine) <<
")";
986void QQuickQmlGenerator::outputShapePath(
const PathNodeInfo &info,
const QPainterPath *painterPath,
const QQuadPath *quadPath, QQuickVectorImageGenerator::PathSelector pathSelector,
const QRectF &boundingRect)
988 Q_UNUSED(pathSelector)
989 Q_ASSERT(painterPath || quadPath);
991 if (Q_UNLIKELY(errorState()))
994 const bool invalidGradientBounds = info.strokeGrad.coordinateMode() == QGradient::ObjectMode
995 && (qFuzzyIsNull(boundingRect.width()) ||
996 qFuzzyIsNull(boundingRect.height()));
998 const QColor strokeColor = info.strokeStyle.color.defaultValue().value<QColor>();
999 const bool noPen = (strokeColor == QColorConstants::Transparent || !strokeColor.isValid())
1000 && !info.strokeStyle.color.isAnimated()
1001 && !info.strokeStyle.opacity.isAnimated()
1002 && (info.strokeGrad.type() == QGradient::NoGradient
1003 || invalidGradientBounds);
1004 if (pathSelector == QQuickVectorImageGenerator::StrokePath && noPen)
1007 const QColor fillColor = info.fillColor.defaultValue().value<QColor>();
1008 const bool noFill = info.grad.type() == QGradient::NoGradient
1009 && fillColor == QColorConstants::Transparent
1010 && !info.fillColor.isAnimated()
1011 && !info.fillOpacity.isAnimated();
1012 if (pathSelector == QQuickVectorImageGenerator::FillPath && noFill)
1015 if (noPen && noFill)
1017 auto fillRule = QQuickShapePath::FillRule(painterPath ? painterPath->fillRule() : quadPath->fillRule());
1018 stream() <<
"ShapePath {";
1021 QString shapePathId = info.id;
1022 if (pathSelector & QQuickVectorImageGenerator::FillPath)
1023 shapePathId += QStringLiteral(
"_fill");
1024 if (pathSelector & QQuickVectorImageGenerator::StrokePath)
1025 shapePathId += QStringLiteral(
"_stroke");
1027 stream() <<
"id: " << shapePathId;
1029 if (!info.nodeId.isEmpty()) {
1030 switch (pathSelector) {
1031 case QQuickVectorImageGenerator::FillPath:
1032 stream() <<
"objectName: \"svg_fill_path:" << info.nodeId <<
"\"";
1034 case QQuickVectorImageGenerator::StrokePath:
1035 stream() <<
"objectName: \"svg_stroke_path:" << info.nodeId <<
"\"";
1037 case QQuickVectorImageGenerator::FillAndStroke:
1038 stream() <<
"objectName: \"svg_path:" << info.nodeId <<
"\"";
1043 if (noPen || !(pathSelector & QQuickVectorImageGenerator::StrokePath)) {
1044 stream() <<
"strokeColor: \"transparent\"";
1046 if (info.strokeGrad.type() != QGradient::NoGradient && !invalidGradientBounds) {
1047 QRectF coordinateSys = info.strokeGrad.coordinateMode() == QGradient::ObjectMode
1049 : QRectF(0.0, 0.0, 1.0, 1.0);
1050 generateGradient(&info.strokeGrad, QStringLiteral(
"strokeGradient"), coordinateSys);
1051 }
else if (info.strokeStyle.opacity.isAnimated()) {
1052 stream() <<
"property color strokeBase: \"" << strokeColor.name(QColor::HexArgb) <<
"\"";
1053 stream() <<
"property real strokeOpacity: " << info.strokeStyle.opacity.defaultValue().toReal();
1054 stream() <<
"strokeColor: Qt.rgba(strokeBase.r, strokeBase.g, strokeBase.b, strokeOpacity)";
1056 stream() <<
"strokeColor: \"" << strokeColor.name(QColor::HexArgb) <<
"\"";
1058 stream() <<
"strokeWidth: " << info.strokeStyle.width;
1059 stream() <<
"capStyle: " << QQuickVectorImageGenerator::Utils::strokeCapStyleString(info.strokeStyle.lineCapStyle);
1060 stream() <<
"joinStyle: " << QQuickVectorImageGenerator::Utils::strokeJoinStyleString(info.strokeStyle.lineJoinStyle);
1061 stream() <<
"miterLimit: " << info.strokeStyle.miterLimit;
1062 if (info.strokeStyle.cosmetic)
1063 stream() <<
"cosmeticStroke: true";
1064 if (info.strokeStyle.dashArray.length() != 0) {
1065 stream() <<
"strokeStyle: " <<
"ShapePath.DashLine";
1066 stream() <<
"dashPattern: " << QQuickVectorImageGenerator::Utils::listString(info.strokeStyle.dashArray);
1067 stream() <<
"dashOffset: " << info.strokeStyle.dashOffset;
1071 QTransform fillTransform = info.fillTransform;
1072 if (!(pathSelector & QQuickVectorImageGenerator::FillPath)) {
1073 stream() <<
"fillColor: \"transparent\"";
1074 }
else if (info.grad.type() != QGradient::NoGradient) {
1075 generateGradient(&info.grad, QStringLiteral(
"fillGradient"));
1078 if (info.grad.coordinateMode() == QGradient::ObjectMode) {
1079 QTransform objectToUserSpace;
1080 objectToUserSpace.translate(boundingRect.x(), boundingRect.y());
1081 objectToUserSpace.scale(boundingRect.width(), boundingRect.height());
1082 fillTransform *= objectToUserSpace;
1085 if (info.fillOpacity.isAnimated()) {
1086 stream() <<
"property color fillBase: \"" << fillColor.name(QColor::HexArgb) <<
"\"";
1087 stream() <<
"property real fillOpacity:" << info.fillOpacity.defaultValue().toReal();
1088 stream() <<
"fillColor: Qt.rgba(fillBase.r, fillBase.g, fillBase.b, fillOpacity)";
1090 stream() <<
"fillColor: \"" << fillColor.name(QColor::HexArgb) <<
"\"";
1094 if (!info.patternId.isEmpty()) {
1095 stream() <<
"fillItem: ShaderEffectSource {";
1098 stream() <<
"parent: " << info.id;
1099 stream() <<
"sourceItem: " << info.patternId;
1100 stream() <<
"hideSource: true";
1101 stream() <<
"visible: false";
1102 stream() <<
"width: " << info.patternId <<
".width";
1103 stream() <<
"height: " << info.patternId <<
".height";
1104 stream() <<
"wrapMode: ShaderEffectSource.Repeat";
1105 stream() <<
"textureSize: Qt.size(width * __qt_toplevel_scale_itemspy.requiredTextureSize.width, "
1106 <<
"height * __qt_toplevel_scale_itemspy.requiredTextureSize.height)";;
1107 stream() <<
"sourceRect: " << info.patternId <<
".sourceRect("
1108 << info.id <<
".width, "
1109 << info.id <<
".height)";
1116 stream() <<
"function calculateFillTransform(xScale, yScale) {";
1119 stream() <<
"var m = ";
1120 generateTransform(fillTransform);
1122 stream() <<
"m.translate(" << info.patternId <<
".sourceOffset("
1123 << info.id <<
".width, "
1124 << info.id <<
".height))";
1126 stream() <<
"m.scale(1.0 / xScale, 1.0 / yScale, 1.0)";
1127 stream() <<
"return m";
1132 stream() <<
"fillTransform: calculateFillTransform(__qt_toplevel_scale_itemspy.requiredTextureSize.width, "
1133 <<
"__qt_toplevel_scale_itemspy.requiredTextureSize.height)";
1135 }
else if (!fillTransform.isIdentity()) {
1136 const QTransform &xf = fillTransform;
1137 stream() <<
"fillTransform: ";
1138 if (info.fillTransform.type() == QTransform::TxTranslate)
1139 stream(SameLine) <<
"PlanarTransform.fromTranslate(" << xf.dx() <<
", " << xf.dy() <<
")";
1140 else if (info.fillTransform.type() == QTransform::TxScale && !xf.dx() && !xf.dy())
1141 stream(SameLine) <<
"PlanarTransform.fromScale(" << xf.m11() <<
", " << xf.m22() <<
")";
1143 generateTransform(xf);
1146 if (info.trim.enabled) {
1147 stream() <<
"trim.start: " << info.trim.start.defaultValue().toReal();
1148 stream() <<
"trim.end: " << info.trim.end.defaultValue().toReal();
1149 stream() <<
"trim.offset: " << info.trim.offset.defaultValue().toReal();
1153 if (fillRule == QQuickShapePath::WindingFill)
1154 stream() <<
"fillRule: ShapePath.WindingFill";
1156 stream() <<
"fillRule: ShapePath.OddEvenFill";
1160 hintStr = QQuickVectorImageGenerator::Utils::pathHintString(*quadPath);
1161 if (!hintStr.isEmpty())
1162 stream() << hintStr;
1164 QQuickAnimatedProperty pathFactor(QVariant::fromValue(0));
1165 pathFactor.setTimelineReferenceId(info.path.timelineReferenceId());
1166 QString pathId = shapePathId +
"_ip"_L1;
1167 if (!info.path.isAnimated() || (info.path.animation(0).startOffset == 0 && info.path.animation(0).isConstant())) {
1168 QString svgPathString = painterPath ? QQuickVectorImageGenerator::Utils::toSvgString(*painterPath) : QQuickVectorImageGenerator::Utils::toSvgString(*quadPath);
1169 stream() <<
"PathSvg { path: \"" << svgPathString <<
"\" }";
1171 stream() <<
"PathInterpolated {";
1173 stream() <<
"id: " << pathId;
1174 stream() <<
"svgPaths: [";
1176 QQuickAnimatedProperty::PropertyAnimation pathFactorAnim = info.path.animation(0);
1177 auto &frames = pathFactorAnim.frames;
1180 for (
auto it = frames.begin(); it != frames.end(); ++it) {
1181 QString svg = QQuickVectorImageGenerator::Utils::toSvgString(it->value<QPainterPath>());
1182 if (svg != lastSvg) {
1184 stream(SameLine) <<
",";
1185 stream() <<
"\"" << svg <<
"\"";
1189 *it = QVariant::fromValue(pathIdx);
1191 pathFactor.addAnimation(pathFactorAnim);
1201 if (pathFactor.isAnimated())
1202 generatePropertyAnimation(pathFactor, pathId,
"factor"_L1);
1204 if (info.trim.enabled) {
1205 generatePropertyAnimation(info.trim.start, shapePathId + QStringLiteral(
".trim"), QStringLiteral(
"start"));
1206 generatePropertyAnimation(info.trim.end, shapePathId + QStringLiteral(
".trim"), QStringLiteral(
"end"));
1207 generatePropertyAnimation(info.trim.offset, shapePathId + QStringLiteral(
".trim"), QStringLiteral(
"offset"));
1210 if (info.strokeStyle.opacity.isAnimated()) {
1211 generatePropertyAnimation(info.strokeStyle.color, shapePathId, QStringLiteral(
"strokeBase"));
1212 generatePropertyAnimation(info.strokeStyle.opacity, shapePathId, QStringLiteral(
"strokeOpacity"));
1214 generatePropertyAnimation(info.strokeStyle.color, shapePathId, QStringLiteral(
"strokeColor"));
1216 if (info.fillOpacity.isAnimated()) {
1217 generatePropertyAnimation(info.fillColor, shapePathId, QStringLiteral(
"fillBase"));
1218 generatePropertyAnimation(info.fillOpacity, shapePathId, QStringLiteral(
"fillOpacity"));
1220 generatePropertyAnimation(info.fillColor, shapePathId, QStringLiteral(
"fillColor"));
1224void QQuickQmlGenerator::generateNode(
const NodeInfo &info)
1226 if (Q_UNLIKELY(errorState() || !isNodeVisible(info)))
1229 stream() <<
"// Missing Implementation for SVG Node: " << info.typeName;
1230 stream() <<
"// Adding an empty Item and skipping";
1231 stream() <<
"Item {";
1233 generateNodeBase(info);
1234 generateNodeEnd(info);
1237void QQuickQmlGenerator::generateTextNode(
const TextNodeInfo &info)
1239 if (Q_UNLIKELY(errorState() || !isNodeVisible(info)))
1242 static int counter = 0;
1243 stream() <<
"Item {";
1245 generateNodeBase(info);
1247 if (!info.isTextArea)
1248 stream() <<
"Item { id: textAlignItem_" << counter <<
"; x: " << info.position.x() <<
"; y: " << info.position.y() <<
"}";
1250 stream() <<
"Text {";
1254 const QString textItemId = QStringLiteral(
"_qt_textItem_%1").arg(counter);
1255 stream() <<
"id: " << textItemId;
1257 generatePropertyAnimation(info.fillColor, textItemId, QStringLiteral(
"color"));
1258 generatePropertyAnimation(info.fillOpacity, textItemId, QStringLiteral(
"color"), AnimationType::ColorOpacity);
1259 generatePropertyAnimation(info.strokeColor, textItemId, QStringLiteral(
"styleColor"));
1260 generatePropertyAnimation(info.strokeOpacity, textItemId, QStringLiteral(
"styleColor"), AnimationType::ColorOpacity);
1262 if (info.isTextArea) {
1263 stream() <<
"x: " << info.position.x();
1264 stream() <<
"y: " << info.position.y();
1265 if (info.size.width() > 0)
1266 stream() <<
"width: " << info.size.width();
1267 if (info.size.height() > 0)
1268 stream() <<
"height: " << info.size.height();
1269 stream() <<
"wrapMode: Text.Wrap";
1270 stream() <<
"clip: true";
1272 QString hAlign = QStringLiteral(
"left");
1273 stream() <<
"anchors.baseline: textAlignItem_" << counter <<
".top";
1274 switch (info.alignment) {
1275 case Qt::AlignHCenter:
1276 hAlign = QStringLiteral(
"horizontalCenter");
1278 case Qt::AlignRight:
1279 hAlign = QStringLiteral(
"right");
1282 qCDebug(lcQuickVectorImage) <<
"Unexpected text alignment" << info.alignment;
1287 stream() <<
"anchors." << hAlign <<
": textAlignItem_" << counter <<
".left";
1291 stream() <<
"color: \"" << info.fillColor.defaultValue().value<QColor>().name(QColor::HexArgb) <<
"\"";
1292 stream() <<
"textFormat:" << (info.needsRichText ?
"Text.RichText" :
"Text.StyledText");
1294 stream() <<
"text: \"" << sanitizeString(info.text) <<
"\"";
1295 stream() <<
"font.family: \"" << sanitizeString(info.font.family()) <<
"\"";
1296 if (info.font.pixelSize() > 0)
1297 stream() <<
"font.pixelSize:" << info.font.pixelSize();
1298 else if (info.font.pointSize() > 0)
1299 stream() <<
"font.pixelSize:" << info.font.pointSizeF();
1300 if (info.font.underline())
1301 stream() <<
"font.underline: true";
1302 if (info.font.weight() != QFont::Normal)
1303 stream() <<
"font.weight: " <<
int(info.font.weight());
1304 if (info.font.italic())
1305 stream() <<
"font.italic: true";
1306 switch (info.font.hintingPreference()) {
1307 case QFont::PreferFullHinting:
1308 stream() <<
"font.hintingPreference: Font.PreferFullHinting";
1310 case QFont::PreferVerticalHinting:
1311 stream() <<
"font.hintingPreference: Font.PreferVerticalHinting";
1313 case QFont::PreferNoHinting:
1314 stream() <<
"font.hintingPreference: Font.PreferNoHinting";
1316 case QFont::PreferDefaultHinting:
1317 stream() <<
"font.hintingPreference: Font.PreferDefaultHinting";
1321 const QColor strokeColor = info.strokeColor.defaultValue().value<QColor>();
1322 if (strokeColor != QColorConstants::Transparent || info.strokeColor.isAnimated()) {
1323 stream() <<
"styleColor: \"" << strokeColor.name(QColor::HexArgb) <<
"\"";
1324 stream() <<
"style: Text.Outline";
1330 generateNodeEnd(info);
1333void QQuickQmlGenerator::generateUseNode(
const UseNodeInfo &info)
1335 if (Q_UNLIKELY(errorState() || !isNodeVisible(info)))
1338 if (info.stage == StructureNodeStage::Start) {
1339 stream() <<
"Item {";
1341 generateNodeBase(info);
1343 generateNodeEnd(info);
1347void QQuickQmlGenerator::generatePathContainer(
const StructureNodeInfo &info)
1350 stream() << shapeName() <<
" {";
1352 if (m_flags.testFlag(QQuickVectorImageGenerator::GeneratorFlag::CurveRenderer))
1353 stream() <<
"preferredRendererType: Shape.CurveRenderer";
1354 if (m_flags.testFlag(QQuickVectorImageGenerator::GeneratorFlag::AsyncShapes))
1355 stream() <<
"asynchronous: true";
1358 m_inShapeItemLevel++;
1361void QQuickQmlGenerator::generateAnimateMotionPath(
const QString &targetName,
1362 const QQuickAnimatedProperty &property)
1364 if (!property.isAnimated())
1367 QPainterPath path = property.defaultValue().value<QVariantList>().value(0).value<QPainterPath>();
1368 const QString mainAnimationId = targetName + QStringLiteral(
"_motion_interpolator");
1369 stream() <<
"PathInterpolator {";
1371 stream() <<
"id: " << mainAnimationId;
1372 const QString svgPathString = QQuickVectorImageGenerator::Utils::toSvgString(path);
1373 stream() <<
"path: Path { PathSvg { path: \"" << svgPathString <<
"\" } }";
1377 generatePropertyAnimation(property, mainAnimationId, QStringLiteral(
"progress"));
1380void QQuickQmlGenerator::generateAnimatedPropertySetter(
const QString &targetName,
1381 const QString &propertyName,
1382 const QVariant &value,
1383 const QQuickAnimatedProperty::PropertyAnimation &animation,
1386 AnimationType animationType)
1388 if (frameTime > 0) {
1389 switch (animationType) {
1390 case AnimationType::Auto:
1391 if (value.typeId() == QMetaType::QColor)
1392 stream() <<
"ColorAnimation {";
1394 stream() <<
"PropertyAnimation {";
1396 case AnimationType::ColorOpacity:
1397 stream() <<
"ColorOpacityAnimation {";
1402 stream() <<
"duration: " << frameTime;
1403 stream() <<
"target: " << targetName;
1404 stream() <<
"property: \"" << propertyName <<
"\"";
1406 if (value.typeId() == QMetaType::QVector3D) {
1407 const QVector3D &v = value.value<QVector3D>();
1408 stream(SameLine) <<
"Qt.vector3d(" << v.x() <<
", " << v.y() <<
", " << v.z() <<
")";
1409 }
else if (value.typeId() == QMetaType::QColor) {
1410 stream(SameLine) <<
"\"" << value.toString() <<
"\"";
1412 stream(SameLine) << value.toReal();
1414 generateEasing(animation, time);
1418 stream() <<
"ScriptAction {";
1420 stream() <<
"script:" << targetName <<
"." << propertyName;
1421 if (animationType == AnimationType::ColorOpacity)
1422 stream(SameLine) <<
".a";
1424 stream(SameLine) <<
" = ";
1425 if (value.typeId() == QMetaType::QVector3D) {
1426 const QVector3D &v = value.value<QVector3D>();
1427 stream(SameLine) <<
"Qt.vector3d(" << v.x() <<
", " << v.y() <<
", " << v.z() <<
")";
1428 }
else if (value.typeId() == QMetaType::QColor) {
1429 stream(SameLine) <<
"\"" << value.toString() <<
"\"";
1431 stream(SameLine) << value.toReal();
1438void QQuickQmlGenerator::generateAnimateTransform(
const QString &targetName,
const NodeInfo &info)
1440 if (!info.transform.isAnimated())
1443 if (usingTimelineAnimation())
1444 return generateTransformTimeline(targetName, info);
1446 const QString mainAnimationId = targetName
1447 + QStringLiteral(
"_transform_animation");
1450 if (Q_UNLIKELY(!isRuntimeGenerator()))
1451 prefix = QStringLiteral(
".animations");
1452 stream() <<
"Connections { target: " << m_topLevelIdString << prefix <<
"; function onRestart() {" << mainAnimationId <<
".restart() } }";
1454 stream() <<
"ParallelAnimation {";
1457 stream() <<
"id:" << mainAnimationId;
1459 generateAnimationBindings();
1460 for (
int groupIndex = 0; groupIndex < info.transform.animationGroupCount(); ++groupIndex) {
1461 int animationStart = info.transform.animationGroup(groupIndex);
1462 int nextAnimationStart = groupIndex + 1 < info.transform.animationGroupCount()
1463 ? info.transform.animationGroup(groupIndex + 1)
1464 : info.transform.animationCount();
1467 const QQuickAnimatedProperty::PropertyAnimation &firstAnimation = info.transform.animation(animationStart);
1468 const bool freeze = firstAnimation.flags & QQuickAnimatedProperty::PropertyAnimation::FreezeAtEnd;
1469 const bool replace = firstAnimation.flags & QQuickAnimatedProperty::PropertyAnimation::ReplacePreviousAnimations;
1471 stream() <<
"SequentialAnimation {";
1474 const int startOffset = processAnimationTime(firstAnimation.startOffset);
1475 if (startOffset > 0)
1476 stream() <<
"PauseAnimation { duration: " << startOffset <<
" }";
1478 const int repeatCount = firstAnimation.repeatCount;
1479 if (repeatCount < 0)
1480 stream() <<
"loops: Animation.Infinite";
1482 stream() <<
"loops: " << repeatCount;
1485 stream() <<
"ScriptAction {";
1488 stream() <<
"script: " << targetName <<
"_transform_base_group"
1489 <<
".activateOverride(" << targetName <<
"_transform_group_" << groupIndex <<
")";
1495 stream() <<
"ParallelAnimation {";
1498 for (
int i = animationStart; i < nextAnimationStart; ++i) {
1499 const QQuickAnimatedProperty::PropertyAnimation &animation = info.transform.animation(i);
1500 if (animation.isConstant())
1502 bool hasRotationCenter =
false;
1503 if (animation.subtype == QTransform::TxRotate) {
1504 for (
auto it = animation.frames.constBegin(); it != animation.frames.constEnd(); ++it) {
1505 const QPointF center = it->value<QVariantList>().value(0).value<QPointF>();
1506 if (!center.isNull()) {
1507 hasRotationCenter =
true;
1513 stream() <<
"SequentialAnimation {";
1516 int previousTime = 0;
1517 QVariantList previousParameters;
1518 for (
auto it = animation.frames.constBegin(); it != animation.frames.constEnd(); ++it) {
1519 const int time = it.key();
1520 const int frameTime = processAnimationTime(time - previousTime);
1521 const QVariantList ¶meters = it.value().value<QVariantList>();
1522 if (parameters.isEmpty())
1525 if (parameters == previousParameters) {
1527 stream() <<
"PauseAnimation { duration: " << frameTime <<
" }";
1529 stream() <<
"ParallelAnimation {";
1532 const QString propertyTargetName = targetName
1533 + QStringLiteral(
"_transform_")
1534 + QString::number(groupIndex)
1535 + QStringLiteral(
"_")
1536 + QString::number(i);
1538 switch (animation.subtype) {
1539 case QTransform::TxTranslate:
1541 const QPointF translation = parameters.first().value<QPointF>();
1543 generateAnimatedPropertySetter(propertyTargetName,
1544 QStringLiteral(
"x"),
1549 generateAnimatedPropertySetter(propertyTargetName,
1550 QStringLiteral(
"y"),
1557 case QTransform::TxScale:
1559 const QPointF scale = parameters.first().value<QPointF>();
1560 generateAnimatedPropertySetter(propertyTargetName,
1561 QStringLiteral(
"xScale"),
1566 generateAnimatedPropertySetter(propertyTargetName,
1567 QStringLiteral(
"yScale"),
1574 case QTransform::TxRotate:
1576 Q_ASSERT(parameters.size() == 2);
1577 const qreal angle = parameters.value(1).toReal();
1578 if (hasRotationCenter) {
1579 const QPointF center = parameters.value(0).value<QPointF>();
1580 generateAnimatedPropertySetter(propertyTargetName,
1581 QStringLiteral(
"origin"),
1582 QVector3D(center.x(), center.y(), 0.0),
1587 generateAnimatedPropertySetter(propertyTargetName,
1588 QStringLiteral(
"angle"),
1595 case QTransform::TxShear:
1597 const QPointF skew = parameters.first().value<QPointF>();
1599 generateAnimatedPropertySetter(propertyTargetName,
1600 QStringLiteral(
"xAngle"),
1606 generateAnimatedPropertySetter(propertyTargetName,
1607 QStringLiteral(
"yAngle"),
1622 previousTime = time;
1623 previousParameters = parameters;
1635 if (firstAnimation.repeatCount >= 0) {
1636 stream() <<
"ScriptAction {";
1639 stream() <<
"script: {";
1643 stream() << targetName <<
"_transform_base_group.deactivate("
1644 << targetName <<
"_transform_group_" << groupIndex <<
")";
1645 }
else if (!replace) {
1646 stream() << targetName <<
"_transform_base_group.deactivateOverride("
1647 << targetName <<
"_transform_group_" << groupIndex <<
")";
1665void QQuickQmlGenerator::generateTransformTimeline(
const QString &targetName,
const NodeInfo &info)
1667 stream() <<
"Timeline {";
1669 stream() <<
"currentFrame: " << info.transform.timelineReferenceId() <<
".frameCounter";
1670 stream() <<
"enabled: true";
1672 const int groupIndex = 0;
1673 for (
int i = 0; i < info.transform.animationCount(); ++i) {
1674 const QQuickAnimatedProperty::PropertyAnimation &animation = info.transform.animation(i);
1675 if (animation.isConstant())
1677 if (info.transform.animationGroupCount() > 1
1678 || animation.repeatCount != 1 || animation.startOffset
1679 || animation.flags != QQuickAnimatedProperty::PropertyAnimation::FreezeAtEnd) {
1680 qCWarning(lcQuickVectorImage) <<
"Feature not implemented in timeline xf animation mode, for"
1681 << targetName <<
"subtype" << animation.subtype;
1684 bool hasRotationCenter =
false;
1685 if (animation.subtype == QTransform::TxRotate) {
1686 for (
auto it = animation.frames.constBegin(); it != animation.frames.constEnd(); ++it) {
1687 const QPointF center = it->value<QVariantList>().value(0).value<QPointF>();
1688 if (!center.isNull()) {
1689 hasRotationCenter =
true;
1695 auto pointFxExtractor = [](
const QVariant &value) {
return value.toPointF().x(); };
1696 auto pointFyExtractor = [](
const QVariant &value) {
return value.toPointF().y(); };
1697 auto realExtractor = [](
const QVariant &value) {
return value.toReal(); };
1698 auto pointFtoVector3dExtractor = [](
const QVariant &v) {
return QVector3D(v.toPointF()); };
1700 const QString propertyTargetName = targetName
1701 + QStringLiteral(
"_transform_")
1702 + QString::number(groupIndex)
1703 + QStringLiteral(
"_")
1704 + QString::number(i);
1706 switch (animation.subtype) {
1707 case QTransform::TxTranslate:
1708 generateTimelinePropertySetter(propertyTargetName,
"x"_L1, animation, pointFxExtractor);
1709 generateTimelinePropertySetter(propertyTargetName,
"y"_L1, animation, pointFyExtractor);
1711 case QTransform::TxScale:
1712 generateTimelinePropertySetter(propertyTargetName,
"xScale"_L1, animation, pointFxExtractor);
1713 generateTimelinePropertySetter(propertyTargetName,
"yScale"_L1, animation, pointFyExtractor);
1715 case QTransform::TxRotate:
1716 if (hasRotationCenter)
1717 generateTimelinePropertySetter(propertyTargetName,
"origin"_L1, animation, pointFtoVector3dExtractor);
1718 generateTimelinePropertySetter(propertyTargetName,
"angle"_L1, animation, realExtractor, 1);
1720 case QTransform::TxShear:
1721 generateTimelinePropertySetter(propertyTargetName,
"xAngle"_L1, animation, pointFxExtractor);
1722 generateTimelinePropertySetter(propertyTargetName,
"yAngle"_L1, animation, pointFyExtractor);
1731bool QQuickQmlGenerator::generateStructureNode(
const StructureNodeInfo &info)
1733 if (Q_UNLIKELY(errorState() || !isNodeVisible(info)))
1736 const bool isPathContainer = !info.forceSeparatePaths && info.isPathContainer;
1737 if (info.stage == StructureNodeStage::Start) {
1738 if (!info.clipBox.isEmpty()) {
1739 stream() <<
"Item { // Clip";
1742 stream() <<
"width: " << info.clipBox.width();
1743 stream() <<
"height: " << info.clipBox.height();
1744 stream() <<
"clip: true";
1747 if (isPathContainer) {
1748 generatePathContainer(info);
1749 }
else if (!info.customItemType.isEmpty()) {
1750 stream() << info.customItemType <<
" {";
1752 stream() <<
"Item { // Structure node";
1756 generateTimelineFields(info);
1758 if (!info.viewBox.isEmpty()) {
1759 stream() <<
"transform: [";
1761 bool translate = !qFuzzyIsNull(info.viewBox.x()) || !qFuzzyIsNull(info.viewBox.y());
1763 stream() <<
"Translate { x: " << -info.viewBox.x() <<
"; y: " << -info.viewBox.y() <<
" },";
1764 stream() <<
"Scale { xScale: width / " << info.viewBox.width() <<
"; yScale: height / " << info.viewBox.height() <<
" }";
1769 generateNodeBase(info);
1771 generateNodeEnd(info);
1772 if (isPathContainer)
1773 m_inShapeItemLevel--;
1775 if (!info.clipBox.isEmpty()) {
1784bool QQuickQmlGenerator::generateMaskNode(
const MaskNodeInfo &info)
1786 if (Q_UNLIKELY(errorState()))
1790 if (info.stage == StructureNodeStage::End) {
1792 startDefsSuffixBlock();
1793 stream() <<
"Loader {";
1796 stream() <<
"id: " << info.id;
1797 stream() <<
"sourceComponent: " << info.id <<
"_container";
1798 stream() <<
"width: item !== null ? item.originalBounds.width : 0";
1799 stream() <<
"height: item !== null ? item.originalBounds.height : 0";
1801 if (info.boundsReferenceId.isEmpty()) {
1802 stream() <<
"property real maskX: " << info.maskRect.left();
1803 stream() <<
"property real maskY: " << info.maskRect.top();
1804 stream() <<
"property real maskWidth: " << info.maskRect.width();
1805 stream() <<
"property real maskHeight: " << info.maskRect.height();
1808 stream() <<
"function maskRect(otherX, otherY, otherWidth, otherHeight) {";
1811 stream() <<
"return ";
1812 if (!info.boundsReferenceId.isEmpty()) {
1813 stream(SameLine) << info.boundsReferenceId <<
".originalBounds";
1814 }
else if (info.isMaskRectRelativeCoordinates) {
1817 << info.id <<
".maskX * otherWidth + otherX,"
1818 << info.id <<
".maskY * otherHeight + otherY,"
1819 << info.id <<
".maskWidth * otherWidth,"
1820 << info.id <<
".maskHeight * otherHeight)";
1824 << info.id <<
".maskX, "
1825 << info.id <<
".maskY, "
1826 << info.id <<
".maskWidth, "
1827 << info.id <<
".maskHeight)";
1836 endDefsSuffixBlock();
1842void QQuickQmlGenerator::generateFilterNode(
const FilterNodeInfo &info)
1844 if (Q_UNLIKELY(errorState()))
1847 stream() <<
"Item {";
1850 generateNodeBase(info);
1852 stream() <<
"property real originalWidth: filterSourceItem.sourceItem.originalBounds.width";
1853 stream() <<
"property real originalHeight: filterSourceItem.sourceItem.originalBounds.height";
1854 stream() <<
"property rect filterRect: " << info.id <<
"_filterParameters"
1855 <<
".adaptToFilterRect(0, 0, originalWidth, originalHeight)";
1857 for (qsizetype i = 0; i < info.steps.size();)
1858 i = generateFilterStep(info, i);
1861 startDefsSuffixBlock();
1862 stream() <<
"QtObject {";
1865 stream() <<
"id: " << info.id <<
"_filterParameters";
1866 stream() <<
"property int wrapMode: ";
1867 if (info.wrapMode == QSGTexture::Repeat)
1868 stream(SameLine) <<
"ShaderEffectSource.Repeat";
1870 stream(SameLine) <<
"ShaderEffectSource.ClampToEdge";
1872 stream() <<
"property rect filterRect: Qt.rect("
1873 << info.filterRect.x() <<
", "
1874 << info.filterRect.y() <<
", "
1875 << info.filterRect.width() <<
", "
1876 << info.filterRect.height() <<
")";
1878 stream() <<
"function adaptToFilterRect(sx, sy, sw, sh) {";
1881 if (info.csFilterRect == FilterNodeInfo::CoordinateSystem::Absolute) {
1882 stream() <<
"return Qt.rect(filterRect.x, filterRect.y, filterRect.width, filterRect.height)";
1884 stream() <<
"return Qt.rect(sx + sw * filterRect.x, sy + sh * filterRect.y, sw * filterRect.width, sh * filterRect.height)";
1892 endDefsSuffixBlock();
1894 generateNodeEnd(info);
1897qsizetype QQuickQmlGenerator::generateFilterStep(
const FilterNodeInfo &info,
1898 qsizetype stepIndex)
1900 const FilterNodeInfo::FilterStep &step = info.steps.at(stepIndex);
1901 const QString primitiveId = info.id + QStringLiteral(
"_primitive") + QString::number(stepIndex);
1905 QString inputId = step.input1 != FilterNodeInfo::FilterInput::SourceColor
1907 : QStringLiteral(
"filterSourceItem");
1909 bool isComposite =
false;
1910 switch (step.filterType) {
1911 case FilterNodeInfo::Type::Merge:
1913 const int maxNodeCount = 8;
1916 QList<QPair<FilterNodeInfo::FilterInput, QString> > inputs;
1917 for (; stepIndex < info.steps.size(); ++stepIndex) {
1918 const FilterNodeInfo::FilterStep &nodeStep = info.steps.at(stepIndex);
1919 if (nodeStep.filterType != FilterNodeInfo::Type::MergeNode)
1922 inputs.append(qMakePair(nodeStep.input1, nodeStep.namedInput1));
1925 if (inputs.size() > maxNodeCount) {
1926 qCWarning(lcQuickVectorImage) <<
"Maximum of" << maxNodeCount
1927 <<
"nodes exceeded in merge effect.";
1930 if (inputs.isEmpty()) {
1931 qCWarning(lcQuickVectorImage) <<
"Merge effect requires at least one node.";
1935 stream() <<
"ShaderEffect {";
1938 stream() <<
"id: " << primitiveId;
1939 stream() <<
"visible: false";
1941 stream() <<
"fragmentShader: \"qrc:/qt-project.org/quickvectorimage/helpers/shaders_ng/femerge.frag.qsb\"";
1942 stream() <<
"width: source1.width";
1943 stream() <<
"height: source1.height";
1944 stream() <<
"property int sourceCount: " << std::min(qsizetype(8), inputs.size());
1946 for (
int i = 0; i < maxNodeCount; ++i) {
1947 auto input = i < inputs.size()
1949 : qMakePair(FilterNodeInfo::FilterInput::None, QStringLiteral(
"null"));
1951 QString inputId = input.first != FilterNodeInfo::FilterInput::SourceColor
1953 : QStringLiteral(
"filterSourceItem");
1955 stream() <<
"property var source" << (i + 1) <<
": " << inputId;
1963 case FilterNodeInfo::Type::CompositeOver:
1964 case FilterNodeInfo::Type::CompositeOut:
1965 case FilterNodeInfo::Type::CompositeIn:
1966 case FilterNodeInfo::Type::CompositeXor:
1967 case FilterNodeInfo::Type::CompositeAtop:
1968 case FilterNodeInfo::Type::CompositeArithmetic:
1969 case FilterNodeInfo::Type::CompositeLighter:
1973 case FilterNodeInfo::Type::BlendNormal:
1974 case FilterNodeInfo::Type::BlendMultiply:
1975 case FilterNodeInfo::Type::BlendScreen:
1976 case FilterNodeInfo::Type::BlendDarken:
1977 case FilterNodeInfo::Type::BlendLighten:
1979 stream() <<
"ShaderEffect {";
1982 QString input2Id = step.input2 != FilterNodeInfo::FilterInput::SourceColor
1984 : QStringLiteral(
"filterSourceItem");
1986 stream() <<
"id: " << primitiveId;
1987 stream() <<
"visible: false";
1990 switch (step.filterType) {
1991 case FilterNodeInfo::Type::CompositeOver:
1992 shader = QStringLiteral(
"fecompositeover");
1994 case FilterNodeInfo::Type::CompositeOut:
1995 shader = QStringLiteral(
"fecompositeout");
1997 case FilterNodeInfo::Type::CompositeIn:
1998 shader = QStringLiteral(
"fecompositein");
2000 case FilterNodeInfo::Type::CompositeXor:
2001 shader = QStringLiteral(
"fecompositexor");
2003 case FilterNodeInfo::Type::CompositeAtop:
2004 shader = QStringLiteral(
"fecompositeatop");
2006 case FilterNodeInfo::Type::CompositeArithmetic:
2007 shader = QStringLiteral(
"fecompositearithmetic");
2009 case FilterNodeInfo::Type::CompositeLighter:
2010 shader = QStringLiteral(
"fecompositelighter");
2012 case FilterNodeInfo::Type::BlendNormal:
2013 shader = QStringLiteral(
"feblendnormal");
2015 case FilterNodeInfo::Type::BlendMultiply:
2016 shader = QStringLiteral(
"feblendmultiply");
2018 case FilterNodeInfo::Type::BlendScreen:
2019 shader = QStringLiteral(
"feblendscreen");
2021 case FilterNodeInfo::Type::BlendDarken:
2022 shader = QStringLiteral(
"feblenddarken");
2024 case FilterNodeInfo::Type::BlendLighten:
2025 shader = QStringLiteral(
"feblendlighten");
2031 stream() <<
"fragmentShader: \"qrc:/qt-project.org/quickvectorimage/helpers/shaders_ng/"
2032 << shader <<
".frag.qsb\"";
2033 stream() <<
"property var source: " << inputId;
2034 stream() <<
"property var source2: " << input2Id;
2035 stream() <<
"width: source.width";
2036 stream() <<
"height: source.height";
2039 QVector4D k = step.filterParameter.value<QVector4D>();
2040 stream() <<
"property var k: Qt.vector4d("
2053 case FilterNodeInfo::Type::Flood:
2055 stream() <<
"Rectangle {";
2058 stream() <<
"id: " << primitiveId;
2059 stream() <<
"visible: false";
2061 stream() <<
"width: " << inputId <<
".width";
2062 stream() <<
"height: " << inputId <<
".height";
2064 QColor floodColor = step.filterParameter.value<QColor>();
2065 stream() <<
"color: \"" << floodColor.name(QColor::HexArgb) <<
"\"";
2072 case FilterNodeInfo::Type::ColorMatrix:
2074 stream() <<
"ShaderEffect {";
2077 stream() <<
"id: " << primitiveId;
2078 stream() <<
"visible: false";
2080 stream() <<
"fragmentShader: \"qrc:/qt-project.org/quickvectorimage/helpers/shaders_ng/fecolormatrix.frag.qsb\"";
2081 stream() <<
"property var source: " << inputId;
2082 stream() <<
"width: source.width";
2083 stream() <<
"height: source.height";
2085 QGenericMatrix<5, 5, qreal> matrix = step.filterParameter.value<QGenericMatrix<5, 5, qreal> >();
2086 for (
int row = 0; row < 4; ++row) {
2089 for (
int col = 0; col < 5; ++col)
2090 stream() <<
"property real m_" << row <<
"_" << col <<
": " << matrix(col, row);
2099 case FilterNodeInfo::Type::Offset:
2101 stream() <<
"ShaderEffectSource {";
2104 stream() <<
"id: " << primitiveId;
2105 stream() <<
"visible: false";
2106 stream() <<
"sourceItem: " << inputId;
2107 stream() <<
"width: sourceItem.width + offset.x";
2108 stream() <<
"height: sourceItem.height + offset.y";
2110 QVector2D offset = step.filterParameter.value<QVector2D>();
2111 stream() <<
"property vector2d offset: Qt.vector2d(";
2112 if (step.csFilterParameter == FilterNodeInfo::CoordinateSystem::Absolute)
2113 stream(SameLine) << offset.x() <<
" / width, " << offset.y() <<
" / height)";
2115 stream(SameLine) << offset.x() <<
", " << offset.y() <<
")";
2117 stream() <<
"sourceRect: Qt.rect(-offset.x, -offset.y, width, height)";
2119 stream() <<
"ItemSpy {";
2121 stream() <<
"id: " << primitiveId <<
"_offset_itemspy";
2122 stream() <<
"anchors.fill: parent";
2126 stream() <<
"textureSize: " << primitiveId <<
"_offset_itemspy.requiredTextureSize";
2135 case FilterNodeInfo::Type::GaussianBlur:
2138 stream() <<
"MultiEffect {";
2141 stream() <<
"id: " << primitiveId;
2142 stream() <<
"visible: false";
2144 stream() <<
"source: " << inputId;
2145 stream() <<
"blurEnabled: true";
2146 stream() <<
"width: source.width";
2147 stream() <<
"height: source.height";
2149 const qreal maxDeviation(12.0);
2150 const qreal deviation = step.filterParameter.toReal();
2151 if (step.csFilterParameter == FilterNodeInfo::CoordinateSystem::Relative)
2152 stream() <<
"blur: Math.min(1.0, " << deviation <<
" * filterSourceItem.width / " << maxDeviation <<
")";
2154 stream() <<
"blur: " << std::min(qreal(1.0), deviation / maxDeviation);
2155 stream() <<
"blurMax: 64";
2163 qCWarning(lcQuickVectorImage) <<
"Unhandled filter type: " <<
int(step.filterType);
2165 stream() <<
"Item { id: " << primitiveId <<
" }";
2170 stream() <<
"ShaderEffectSource {";
2173 stream() <<
"id: " << step.outputName;
2174 if (stepIndex < info.steps.size())
2175 stream() <<
"visible: false";
2177 qreal x1, x2, y1, y2;
2178 step.filterPrimitiveRect.getCoords(&x1, &y1, &x2, &y2);
2179 if (step.csFilterParameter == FilterNodeInfo::CoordinateSystem::Absolute) {
2180 stream() <<
"property real fpx1: " << x1;
2181 stream() <<
"property real fpy1: " << y1;
2182 stream() <<
"property real fpx2: " << x2;
2183 stream() <<
"property real fpy2: " << y2;
2184 }
else if (step.csFilterParameter == FilterNodeInfo::CoordinateSystem::Relative) {
2188 stream() <<
"property real fpx1: " << x1 <<
" * filterSourceItem.sourceItem.originalBounds.width";
2189 stream() <<
"property real fpy1: " << y1 <<
" * filterSourceItem.sourceItem.originalBounds.height";
2190 stream() <<
"property real fpx2: " << x2 <<
" * filterSourceItem.sourceItem.originalBounds.width";
2191 stream() <<
"property real fpy2: " << y2 <<
" * filterSourceItem.sourceItem.originalBounds.height";
2193 stream() <<
"property real fpx1: parent.filterRect.x";
2194 stream() <<
"property real fpy1: parent.filterRect.y";
2195 stream() <<
"property real fpx2: parent.filterRect.x + parent.filterRect.width";
2196 stream() <<
"property real fpy2: parent.filterRect.y + parent.filterRect.height";
2199 stream() <<
"sourceItem: " << primitiveId;
2200 stream() <<
"sourceRect: Qt.rect(fpx1 - parent.filterRect.x, fpy1 - parent.filterRect.y, width, height)";
2202 stream() <<
"x: fpx1";
2203 stream() <<
"y: fpy1";
2204 stream() <<
"width: " <<
"fpx2 - fpx1";
2205 stream() <<
"height: " <<
"fpy2 - fpy1";
2207 stream() <<
"ItemSpy {";
2209 stream() <<
"id: " << primitiveId <<
"_itemspy";
2210 stream() <<
"anchors.fill: parent";
2214 stream() <<
"textureSize: " << primitiveId <<
"_itemspy.requiredTextureSize";
2222void QQuickQmlGenerator::generateTimelineFields(
const StructureNodeInfo &info)
2224 if (usingTimelineAnimation() && info.timelineInfo) {
2225 QString frameCounterRef = info.timelineInfo->frameCounterReference
2226 + QStringLiteral(
".frameCounter");
2228 if (info.timelineInfo->generateVisibility) {
2229 stream() <<
"visible: " << frameCounterRef <<
" >= " << info.timelineInfo->startFrame
2230 <<
" && " << frameCounterRef <<
" < " << info.timelineInfo->endFrame;
2233 if (info.timelineInfo->generateFrameCounter) {
2234 stream() <<
"property real frameCounter: ";
2235 if (info.timelineInfo->frameCounterMapper.isAnimated()) {
2237 stream(SameLine) <<
"0";
2238 generatePropertyTimeline(info.timelineInfo->frameCounterMapper, info.id,
"frameCounter"_L1);
2240 const auto offset = info.timelineInfo->frameCounterOffset;
2241 const auto multiplier = info.timelineInfo->frameCounterMultiplier;
2242 const bool needsParens = (offset && multiplier);
2244 stream(SameLine) <<
"(";
2245 stream(SameLine) << frameCounterRef;
2247 stream(SameLine) << (offset > 0 ?
" + " :
" - ") << qAbs(offset);
2249 stream(SameLine) <<
")";
2251 stream(SameLine) <<
" * " << multiplier;
2257bool QQuickQmlGenerator::generatePatternNode(
const PatternNodeInfo &info)
2259 if (info.stage == StructureNodeStage::Start) {
2262 startDefsSuffixBlock();
2263 stream() <<
"Loader {";
2266 stream() <<
"id: " << info.id;
2267 stream() <<
"sourceComponent: " << info.id <<
"_container";
2268 stream() <<
"width: item !== null ? item.originalBounds.width : 0";
2269 stream() <<
"height: item !== null ? item.originalBounds.height : 0";
2270 stream() <<
"visible: false";
2271 stream() <<
"function sourceRect(targetWidth, targetHeight) {";
2274 stream() <<
"return Qt.rect(0, 0, ";
2275 if (!info.isPatternRectRelativeCoordinates) {
2276 stream(SameLine) << info.patternRect.width() <<
", "
2277 << info.patternRect.height();
2279 stream(SameLine) << info.patternRect.width() <<
" * targetWidth, "
2280 << info.patternRect.height() <<
" * targetHeight";
2282 stream(SameLine) <<
")";
2286 stream() <<
"function sourceOffset(targetWidth, targetHeight) {";
2289 stream() <<
"return Qt.vector3d(";
2290 if (!info.isPatternRectRelativeCoordinates) {
2291 stream(SameLine) << info.patternRect.x() <<
", "
2292 << info.patternRect.y() <<
", ";
2294 stream(SameLine) << info.patternRect.x() <<
" * targetWidth, "
2295 << info.patternRect.y() <<
" * targetHeight, ";
2297 stream(SameLine) <<
"0.0)";
2305 endDefsSuffixBlock();
2311void QQuickQmlGenerator::generateDefsInstantiationNode(
const StructureNodeInfo &info)
2313 if (Q_UNLIKELY(errorState()))
2316 if (info.stage == StructureNodeStage::Start) {
2317 stream() <<
"Loader {";
2320 stream() <<
"sourceComponent: " << info.defsId <<
"_container";
2321 generateNodeBase(info);
2322 generateTimelineFields(info);
2329bool QQuickQmlGenerator::generateMarkerNode(
const MarkerNodeInfo &info)
2331 if (info.stage == StructureNodeStage::Start) {
2332 startDefsSuffixBlock();
2333 stream() <<
"QtObject {";
2336 stream() <<
"id: " << info.id <<
"_markerParameters";
2338 stream() <<
"property bool startReversed: ";
2339 if (info.orientation == MarkerNodeInfo::Orientation::AutoStartReverse)
2340 stream(SameLine) <<
"true";
2342 stream(SameLine) <<
"false";
2344 stream() <<
"function autoAngle(adaptedAngle) {";
2346 if (info.orientation == MarkerNodeInfo::Orientation::Value)
2347 stream() <<
"return " << info.angle;
2349 stream() <<
"return adaptedAngle";
2355 endDefsSuffixBlock();
2357 if (!info.clipBox.isEmpty()) {
2358 stream() <<
"Item {";
2361 stream() <<
"x: " << info.clipBox.x();
2362 if (info.markerUnits == MarkerNodeInfo::MarkerUnits::StrokeWidth)
2363 stream(SameLine) <<
" * strokeWidth";
2364 stream() <<
"y: " << info.clipBox.y();
2365 if (info.markerUnits == MarkerNodeInfo::MarkerUnits::StrokeWidth)
2366 stream(SameLine) <<
" * strokeWidth";
2367 stream() <<
"width: " << info.clipBox.width();
2368 if (info.markerUnits == MarkerNodeInfo::MarkerUnits::StrokeWidth)
2369 stream(SameLine) <<
" * strokeWidth";
2370 stream() <<
"height: " << info.clipBox.height();
2371 if (info.markerUnits == MarkerNodeInfo::MarkerUnits::StrokeWidth)
2372 stream(SameLine) <<
" * strokeWidth";
2373 stream() <<
"clip: true";
2376 stream() <<
"Item {";
2379 if (!info.clipBox.isEmpty()) {
2380 stream() <<
"x: " << -info.clipBox.x();
2381 if (info.markerUnits == MarkerNodeInfo::MarkerUnits::StrokeWidth)
2382 stream(SameLine) <<
" * strokeWidth";
2383 stream() <<
"y: " << -info.clipBox.y();
2384 if (info.markerUnits == MarkerNodeInfo::MarkerUnits::StrokeWidth)
2385 stream(SameLine) <<
" * strokeWidth";
2388 stream() <<
"id: " << info.id;
2390 stream() <<
"property real markerWidth: " << info.markerSize.width();
2391 if (info.markerUnits == MarkerNodeInfo::MarkerUnits::StrokeWidth)
2392 stream(SameLine) <<
" * strokeWidth";
2394 stream() <<
"property real markerHeight: " << info.markerSize.height();
2395 if (info.markerUnits == MarkerNodeInfo::MarkerUnits::StrokeWidth)
2396 stream(SameLine) <<
" * strokeWidth";
2398 stream() <<
"function calculateMarkerScale(w, h) {";
2401 stream() <<
"var scaleX = 1.0";
2402 stream() <<
"var scaleY = 1.0";
2403 stream() <<
"var offsetX = 0.0";
2404 stream() <<
"var offsetY = 0.0";
2405 if (info.viewBox.width() > 0)
2406 stream() <<
"if (w > 0) scaleX = w / " << info.viewBox.width();
2407 if (info.viewBox.height() > 0)
2408 stream() <<
"if (h > 0) scaleY = h / " << info.viewBox.height();
2410 if (info.preserveAspectRatio & MarkerNodeInfo::xyMask) {
2411 stream() <<
"if (scaleX != scaleY) {";
2414 if (info.preserveAspectRatio & MarkerNodeInfo::meet)
2415 stream() <<
"scaleX = scaleY = Math.min(scaleX, scaleY)";
2417 stream() <<
"scaleX = scaleY = Math.max(scaleX, scaleY)";
2419 QString overflowX = QStringLiteral(
"scaleX * %1 - w").arg(info.viewBox.width());
2420 QString overflowY = QStringLiteral(
"scaleY * %1 - h").arg(info.viewBox.height());
2422 const quint8 xRatio = info.preserveAspectRatio & MarkerNodeInfo::xMask;
2423 if (xRatio == MarkerNodeInfo::xMid)
2424 stream() <<
"offsetX -= " << overflowX <<
" / 2";
2425 else if (xRatio == MarkerNodeInfo::xMax)
2426 stream() <<
"offsetX -= " << overflowX;
2428 const quint8 yRatio = info.preserveAspectRatio & MarkerNodeInfo::yMask;
2429 if (yRatio == MarkerNodeInfo::yMid)
2430 stream() <<
"offsetY -= " << overflowY <<
" / 2";
2431 else if (yRatio == MarkerNodeInfo::yMax)
2432 stream() <<
"offsetY -= " << overflowY;
2438 stream() <<
"return Qt.vector4d("
2439 <<
"offsetX - " << info.anchorPoint.x() <<
" * scaleX, "
2440 <<
"offsetY - " << info.anchorPoint.y() <<
" * scaleY, "
2447 stream() <<
"property vector4d markerScale: calculateMarkerScale(markerWidth, markerHeight)";
2449 stream() <<
"transform: [";
2452 stream() <<
"Scale { xScale: " << info.id <<
".markerScale.z; yScale: " << info.id <<
".markerScale.w },";
2453 stream() <<
"Translate { x: " << info.id <<
".markerScale.x; y: " << info.id <<
".markerScale.y }";
2459 generateNodeEnd(info);
2461 if (!info.clipBox.isEmpty()) {
2470bool QQuickQmlGenerator::generateRootNode(
const StructureNodeInfo &info)
2472 if (Q_UNLIKELY(errorState()))
2475 const QStringList comments = m_commentString.split(u'\n');
2477 if (!isNodeVisible(info)) {
2480 if (comments.isEmpty()) {
2481 stream() <<
"// Generated from SVG";
2483 for (
const auto &comment : comments)
2484 stream() <<
"// " << comment;
2487 stream() <<
"import QtQuick";
2488 stream() <<
"import QtQuick.Shapes" << Qt::endl;
2489 stream() <<
"Item {";
2492 double w = info.size.width();
2493 double h = info.size.height();
2495 stream() <<
"implicitWidth: " << w;
2497 stream() <<
"implicitHeight: " << h;
2505 if (info.stage == StructureNodeStage::Start) {
2508 if (comments.isEmpty())
2509 stream() <<
"// Generated from SVG";
2511 for (
const auto &comment : comments)
2512 stream() <<
"// " << comment;
2514 stream() <<
"import QtQuick";
2515 stream() <<
"import QtQuick.VectorImage";
2516 stream() <<
"import QtQuick.VectorImage.Helpers";
2517 stream() <<
"import QtQuick.Shapes";
2518 stream() <<
"import QtQuick.Effects";
2519 if (usingTimelineAnimation())
2520 stream() <<
"import QtQuick.Timeline";
2522 for (
const auto &import : std::as_const(m_extraImports))
2523 stream() <<
"import " << import;
2525 stream() << Qt::endl <<
"Item {";
2528 double w = info.size.width();
2529 double h = info.size.height();
2531 stream() <<
"implicitWidth: " << w;
2533 stream() <<
"implicitHeight: " << h;
2535 if (Q_UNLIKELY(!isRuntimeGenerator())) {
2536 stream() <<
"component AnimationsInfo : QtObject";
2541 stream() <<
"property bool paused: false";
2542 stream() <<
"property int loops: 1";
2543 stream() <<
"signal restart()";
2545 if (Q_UNLIKELY(!isRuntimeGenerator())) {
2548 stream() <<
"property AnimationsInfo animations : AnimationsInfo {}";
2551 stream() <<
"Item {";
2553 stream() <<
"width: 1";
2554 stream() <<
"height: 1";
2556 stream() <<
"ItemSpy { id: __qt_toplevel_scale_itemspy; anchors.fill: parent }";
2561 if (!info.viewBox.isEmpty()) {
2562 stream() <<
"transform: [";
2564 bool translate = !qFuzzyIsNull(info.viewBox.x()) || !qFuzzyIsNull(info.viewBox.y());
2566 stream() <<
"Translate { x: " << -info.viewBox.x() <<
"; y: " << -info.viewBox.y() <<
" },";
2567 stream() <<
"Scale { xScale: width / " << info.viewBox.width() <<
"; yScale: height / " << info.viewBox.height() <<
" }";
2572 if (!info.forceSeparatePaths && info.isPathContainer) {
2573 m_topLevelIdString = QStringLiteral(
"__qt_toplevel");
2574 stream() <<
"id: " << m_topLevelIdString;
2576 generatePathContainer(info);
2579 generateNodeBase(info);
2581 m_topLevelIdString = generateNodeBase(info);
2582 if (m_topLevelIdString.isEmpty())
2583 qCWarning(lcQuickVectorImage) <<
"No ID specified for top level item";
2586 if (usingTimelineAnimation() && info.timelineInfo) {
2587 stream() <<
"property real frameCounter: " << info.timelineInfo->startFrame;
2588 stream() <<
"NumberAnimation on frameCounter {";
2590 stream() <<
"from: " << info.timelineInfo->startFrame;
2591 stream() <<
"to: " << info.timelineInfo->endFrame - 0.01;
2592 stream() <<
"duration: " << processAnimationTime(info.timelineInfo->duration);
2593 generateAnimationBindings();
2596 stream() <<
"visible: frameCounter >= " << info.timelineInfo->startFrame
2597 <<
" && frameCounter < " << info.timelineInfo->endFrame;
2600 if (m_inShapeItemLevel > 0) {
2601 m_inShapeItemLevel--;
2606 for (
const auto [coords, id] : m_easings.asKeyValueRange()) {
2607 stream() <<
"readonly property easingCurve " << id <<
": ({ type: Easing.BezierSpline, bezierCurve: [ ";
2608 for (
auto coord : coords)
2609 stream(SameLine) << coord <<
", ";
2610 stream(SameLine) <<
"1, 1 ] })";
2613 generateNodeEnd(info);
2620void QQuickQmlGenerator::startDefsSuffixBlock()
2622 int tmp = m_oldIndentLevels.top();
2623 m_oldIndentLevels.push(m_indentLevel);
2624 m_indentLevel = tmp;
2625 m_stream.setString(&m_defsSuffix);
2628void QQuickQmlGenerator::endDefsSuffixBlock()
2630 m_indentLevel = m_oldIndentLevels.pop();
2631 m_stream.setDevice(&m_result);
2634QStringView QQuickQmlGenerator::indent()
2636 static QString indentString;
2637 int indentWidth = m_indentLevel * 4;
2638 if (indentWidth > indentString.size())
2639 indentString.fill(QLatin1Char(
' '), indentWidth * 2);
2640 return QStringView(indentString).first(indentWidth);
2643QTextStream &QQuickQmlGenerator::stream(
int flags)
2645 if (m_stream.device() ==
nullptr && m_stream.string() ==
nullptr)
2646 m_stream.setDevice(&m_result);
2647 else if (!(flags & StreamFlags::SameLine))
2648 m_stream << Qt::endl << indent();
2650 static qint64 maxBufferSize = qEnvironmentVariableIntegerValue(
"QT_QUICKVECTORIMAGE_MAX_BUFFER").value_or(64 << 20);
2651 if (m_stream.device()) {
2652 if (Q_UNLIKELY(!checkSanityLimit(m_stream.device()->size(), maxBufferSize,
"buffer size"_L1)))
2653 m_stream.device()->reset();
2655 if (Q_UNLIKELY(!checkSanityLimit(m_stream.string()->size(), maxBufferSize,
"buffer string size"_L1)))
2656 m_stream.string()->clear();
2662const char *QQuickQmlGenerator::shapeName()
const
2664 return m_shapeTypeName.isEmpty() ?
"Shape" : m_shapeTypeName.constData();
Combined button and popup list for selecting options.
static QT_BEGIN_NAMESPACE QString sanitizeString(const QString &input)
static int processAnimationTime(int time)