Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qsvgvisitorimpl.cpp
Go to the documentation of this file.
1// Copyright (C) 2024 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
7
8#include <private/qsvgvisitor_p.h>
9
10#include <QString>
11#include <QPainter>
12#include <QTextDocument>
13#include <QTextLayout>
14#include <QMatrix4x4>
15#include <QQuickItem>
16
17#include <private/qquickshape_p.h>
18#include <private/qquicktext_p.h>
19#include <private/qquicktranslate_p.h>
20#include <private/qquickitem_p.h>
21
22#include <private/qquickimagebase_p_p.h>
23#include <private/qquickimage_p.h>
24#include <private/qsgcurveprocessor_p.h>
25
26#include <private/qquadpath_p.h>
27
28#include <QtCore/private/qstringiterator_p.h>
29
30#include "utils_p.h"
31#include <QtCore/qloggingcategory.h>
32
33#include <QtSvg/private/qsvgstyle_p.h>
34#include <QtSvg/private/qsvgfilter_p.h>
35
37
38Q_STATIC_LOGGING_CATEGORY(lcVectorImageAnimations, "qt.quick.vectorimage.animations")
39
40using namespace Qt::StringLiterals;
41
43{
44public:
46 {
47 m_dummyImage = QImage(1, 1, QImage::Format_RGB32);
48 m_dummyPainter.begin(&m_dummyImage);
49 QPen defaultPen(Qt::NoBrush, 1, Qt::SolidLine, Qt::FlatCap, Qt::SvgMiterJoin);
50 defaultPen.setMiterLimit(4);
51 m_dummyPainter.setPen(defaultPen);
52 m_dummyPainter.setBrush(Qt::black);
53 }
54
56 {
57 m_dummyPainter.end();
58 }
59
60 QPainter& painter() { return m_dummyPainter; }
61 QSvgExtraStates& states() { return m_svgState; }
62
64 {
65 if (m_dummyPainter.brush().style() == Qt::NoBrush ||
66 m_dummyPainter.brush().color() == QColorConstants::Transparent) {
67 return QColor(QColorConstants::Transparent);
68 }
69
70 QColor fillColor;
71 fillColor = m_dummyPainter.brush().color();
72 fillColor.setAlphaF(m_svgState.fillOpacity);
73
74 return fillColor;
75 }
76
78 {
79 return m_svgState.fillOpacity;
80 }
81
82 const QGradient *currentStrokeGradient() const
83 {
84 QBrush brush = m_dummyPainter.pen().brush();
85 if (brush.style() == Qt::LinearGradientPattern
86 || brush.style() == Qt::RadialGradientPattern
87 || brush.style() == Qt::ConicalGradientPattern) {
88 return brush.gradient();
89 }
90 return nullptr;
91 }
92
93 const QGradient *currentFillGradient() const
94 {
95 if (m_dummyPainter.brush().style() == Qt::LinearGradientPattern || m_dummyPainter.brush().style() == Qt::RadialGradientPattern || m_dummyPainter.brush().style() == Qt::ConicalGradientPattern )
96 return m_dummyPainter.brush().gradient();
97 return nullptr;
98 }
99
101 {
102 return m_dummyPainter.brush().transform();
103 }
104
106 {
107 if (m_dummyPainter.pen().brush().style() == Qt::NoBrush ||
108 m_dummyPainter.pen().brush().color() == QColorConstants::Transparent) {
109 return QColor(QColorConstants::Transparent);
110 }
111
112 QColor strokeColor;
113 strokeColor = m_dummyPainter.pen().brush().color();
114 strokeColor.setAlphaF(m_svgState.strokeOpacity);
115
116 return strokeColor;
117 }
118
119 static QGradient applyOpacityToGradient(const QGradient &gradient, float opacity)
120 {
121 QGradient grad = gradient;
122 QGradientStops stops;
123 for (auto &stop : grad.stops()) {
124 stop.second.setAlphaF(stop.second.alphaF() * opacity);
125 stops.append(stop);
126 }
127
128 grad.setStops(stops);
129
130 return grad;
131 }
132
133 float currentStrokeWidth() const
134 {
135 float penWidth = m_dummyPainter.pen().widthF();
136 return penWidth ? penWidth : 1;
137 }
138
140 {
141 return m_dummyPainter.pen();
142 }
143
144protected:
148};
149
150namespace {
151inline bool isPathContainer(const QSvgStructureNode *node)
152{
153 bool foundPath = false;
154 for (const auto &child : node->renderers()) {
155 switch (child->type()) {
156 // nodes that shouldn't go inside Shape{}
157 case QSvgNode::Switch:
158 case QSvgNode::Doc:
159 case QSvgNode::Group:
160 case QSvgNode::AnimateColor:
161 case QSvgNode::AnimateTransform:
162 case QSvgNode::Use:
163 case QSvgNode::Video:
164 case QSvgNode::Image:
165 case QSvgNode::Textarea:
166 case QSvgNode::Text:
167 case QSvgNode::Tspan:
168 case QSvgNode::Mask:
169 case QSvgNode::Marker:
170 case QSvgNode::Pattern:
171 //qCDebug(lcQuickVectorGraphics) << "NOT path container because" << node->typeName() ;
172 return false;
173
174 // nodes that could go inside Shape{}
175 case QSvgNode::Defs:
176 case QSvgNode::Symbol:
177 break;
178
179 // nodes that are done as pure ShapePath{}
180 case QSvgNode::Rect:
181 case QSvgNode::Circle:
182 case QSvgNode::Ellipse:
183 case QSvgNode::Line:
184 case QSvgNode::Path:
185 case QSvgNode::Polygon:
186 case QSvgNode::Polyline:
187 {
188 if (child->hasFilter())
189 return false;
190
191 if (child->hasAnyMarker())
192 return false;
193
194 if (!child->style().opacity.isDefault())
195 return false;
196
197 if (!child->style().transform.isDefault()) {
198 //qCDebug(lcQuickVectorGraphics) << "NOT path container because local transform";
199 return false;
200 }
201 const auto animations = child->document()->animator()->animationsForNode(child.get());
202 if (!animations.isEmpty()) {
203 //qCDebug(lcQuickVectorGraphics) << "NOT path container because local transform animation";
204 return false;
205 }
206 foundPath = true;
207 break;
208 }
209 default:
210 qCDebug(lcQuickVectorImage) << "Unhandled type in switch" << child->type();
211 break;
212 }
213 }
214 //qCDebug(lcQuickVectorGraphics) << "Container" << node->nodeId() << node->typeName() << "is" << foundPath;
215 return foundPath;
216}
217
218static QString capStyleName(Qt::PenCapStyle style)
219{
220 QString styleName;
221
222 switch (style) {
223 case Qt::SquareCap:
224 styleName = QStringLiteral("squarecap");
225 break;
226 case Qt::FlatCap:
227 styleName = QStringLiteral("flatcap");
228 break;
229 case Qt::RoundCap:
230 styleName = QStringLiteral("roundcap");
231 break;
232 default:
233 break;
234 }
235
236 return styleName;
237}
238
239static QString joinStyleName(Qt::PenJoinStyle style)
240{
241 QString styleName;
242
243 switch (style) {
244 case Qt::MiterJoin:
245 styleName = QStringLiteral("miterjoin");
246 break;
247 case Qt::BevelJoin:
248 styleName = QStringLiteral("beveljoin");
249 break;
250 case Qt::RoundJoin:
251 styleName = QStringLiteral("roundjoin");
252 break;
253 case Qt::SvgMiterJoin:
254 styleName = QStringLiteral("svgmiterjoin");
255 break;
256 default:
257 break;
258 }
259
260 return styleName;
261}
262
263static QString dashArrayString(QList<qreal> dashArray)
264{
265 if (dashArray.isEmpty())
266 return QString();
267
268 QString dashArrayString;
269 QTextStream stream(&dashArrayString);
270
271 for (int i = 0; i < dashArray.length() - 1; i++) {
272 qreal value = dashArray[i];
273 stream << value << ", ";
274 }
275
276 stream << dashArray.last();
277
278 return dashArrayString;
279}
280};
281
282static QString scrub(const QString &raw)
283{
284 QString res(raw.left(80));
285
286 if (!res.isEmpty()) {
287 constexpr QLatin1StringView legalSymbols("_-.:"); // Only valid SVG id characters
288 qsizetype i = 0;
289 do {
290 if (res.at(i).isLetterOrNumber() || legalSymbols.contains(res.at(i)))
291 i++;
292 else
293 res.remove(i, 1);
294 } while (i < res.size());
295 }
296
297 return res;
298}
299
300QSvgVisitorImpl::QSvgVisitorImpl(const QString svgFileName,
301 QQuickGenerator *generator,
302 bool assumeTrustedSource)
305 , m_assumeTrustedSource(assumeTrustedSource)
307{
308}
309
310QSvgVisitorImpl::~QSvgVisitorImpl() = default;
311
312bool QSvgVisitorImpl::startDefsBlock(const QSvgNode *node)
313{
315 fillCommonNodeInfo(node, info);
316
318
319 // Pattern transforms handled through the fill transform, the transform property is ignored, so
320 // we overwrite it with identity.
321 if (node->type() == QSvgNode::Pattern) {
322 info.transform = QQuickAnimatedProperty(QVariant::fromValue(QTransform{}));
323 info.isDefaultTransform = true;
324 }
325
326 if (!m_generator->generateDefsNode(info))
327 return false;
328
329 return true;
330}
331
332void QSvgVisitorImpl::endDefsBlock(const QSvgNode *node)
333{
335 fillCommonNodeInfo(node, info);
336
338
339 m_generator->generateDefsNode(info);
340}
341
342static inline bool isStructureNode(const QSvgNode *node)
343{
344 switch (node->type()) {
345 case QSvgNode::Switch:
346 case QSvgNode::Doc:
347 case QSvgNode::Defs:
348 case QSvgNode::Group:
349 case QSvgNode::Mask:
350 case QSvgNode::Symbol:
351 case QSvgNode::Filter:
352 case QSvgNode::FeMerge:
353 case QSvgNode::FeMergenode:
354 case QSvgNode::FeColormatrix:
355 case QSvgNode::FeGaussianblur:
356 case QSvgNode::FeOffset:
357 case QSvgNode::FeComposite:
358 case QSvgNode::FeFlood:
359 case QSvgNode::FeBlend:
360 case QSvgNode::Marker:
361 case QSvgNode::Pattern:
362 return true;
363 default:
364 return false;
365 }
366}
367
368static void recurseSvgNodes(const QSvgNode *root, const std::function<void(const QSvgNode *)> &fnc)
369{
370 fnc(root);
371
372 if (isStructureNode(root)) {
373 const QSvgStructureNode *sn = static_cast<const QSvgStructureNode *>(root);
374 for (const auto &child : sn->renderers())
375 recurseSvgNodes(child.get(), fnc);
376 }
377}
378
379void QSvgVisitorImpl::pregenerateReferencedNodes(const QSvgNode *doc)
380{
381 Q_ASSERT(m_generator != nullptr);
382
383 // Find any node which is referenced from elsewhere and generate a Component definition
384 // for it
385 QSet<QString> referencedIds;
386 auto findReferencedIds = [&referencedIds](const QSvgNode *node) {
387 if (node->hasFilter())
388 referencedIds.insert(node->filterId());
389 if (node->hasMask())
390 referencedIds.insert(node->maskId());
391 if (node->hasMarkerStart())
392 referencedIds.insert(node->markerStartId());
393 if (node->hasMarkerMid())
394 referencedIds.insert(node->markerMidId());
395 if (node->hasMarkerEnd())
396 referencedIds.insert(node->markerEndId());
397 if (node->type() == QSvgNode::Pattern)
398 referencedIds.insert(node->nodeId());
399 };
400 recurseSvgNodes(doc, findReferencedIds);
401
402 m_pregeneratingReferencedNodes = true;
403 for (const QString &referencedId : referencedIds) {
404 const QSvgNode *referencedNode = doc->document()->namedNode(referencedId);
405 if (referencedNode == nullptr)
406 continue;
407
408 if (!startDefsBlock(referencedNode))
409 return;
410
411 traverse(referencedNode);
412
413 endDefsBlock(referencedNode);
414 }
415
416 m_pregeneratingReferencedNodes = false;
417}
418
420{
421 if (!m_generator) {
422 qCDebug(lcQuickVectorImage) << "No valid QQuickGenerator is set. Genration will stop";
423 return false;
424 }
425
426 QtSvg::Options options;
427 if (m_assumeTrustedSource)
428 options.setFlag(QtSvg::AssumeTrustedSource);
429
430 const auto doc = QSvgDocument::load(m_svgFileName, options);
431 if (!doc) {
432 qCDebug(lcQuickVectorImage) << "Not a valid Svg File : " << m_svgFileName;
433 return false;
434 }
435
436 QSvgVisitor::traverse(doc.get());
437
438 return true;
439}
440
441void QSvgVisitorImpl::visitNode(const QSvgNode *node)
442{
443 handleBaseNodeSetup(node);
444
445 NodeInfo info;
446 fillCommonNodeInfo(node, info);
447 fillAnimationInfo(node, info);
448
449 m_generator->generateNode(info);
450
451 handleBaseNodeEnd(node);
452}
453
454void QSvgVisitorImpl::visitImageNode(const QSvgImage *node)
455{
456 // TODO: this requires proper asset management.
457 handleBaseNodeSetup(node);
458
459 ImageNodeInfo info;
460 fillCommonNodeInfo(node, info);
461 fillAnimationInfo(node, info);
462 info.image = node->image();
463 info.rect = node->rect();
464 info.externalFileReference = node->filename();
465
466 m_generator->generateImageNode(info);
467
468 handleBaseNodeEnd(node);
469}
470
471void QSvgVisitorImpl::visitRectNode(const QSvgRect *node)
472{
473 QRectF rect = node->rect();
474 QPointF rads = node->radius();
475 // This is using Qt::RelativeSize semantics: percentage of half rect size
476 qreal x1 = rect.left();
477 qreal x2 = rect.right();
478 qreal y1 = rect.top();
479 qreal y2 = rect.bottom();
480
481 qreal rx = rads.x() * rect.width() / 200;
482 qreal ry = rads.y() * rect.height() / 200;
483 QPainterPath p;
484
485 p.moveTo(x1 + rx, y1);
486 p.lineTo(x2 - rx, y1);
487 // qCDebug(lcQuickVectorGraphics) << "Line1" << x2 - rx << y1;
488 p.arcTo(x2 - rx * 2, y1, rx * 2, ry * 2, 90, -90); // ARC to x2, y1 + ry
489 // qCDebug(lcQuickVectorGraphics) << "p1" << p;
490
491 p.lineTo(x2, y2 - ry);
492 p.arcTo(x2 - rx * 2, y2 - ry * 2, rx * 2, ry * 2, 0, -90); // ARC to x2 - rx, y2
493
494 p.lineTo(x1 + rx, y2);
495 p.arcTo(x1, y2 - ry * 2, rx * 2, ry * 2, 270, -90); // ARC to x1, y2 - ry
496
497 p.lineTo(x1, y1 + ry);
498 p.arcTo(x1, y1, rx * 2, ry * 2, 180, -90); // ARC to x1 + rx, y1
499
500 handlePathNode(node, p);
501}
502
503void QSvgVisitorImpl::visitEllipseNode(const QSvgEllipse *node)
504{
505 QRectF rect = node->rect();
506
507 QPainterPath p;
508 p.addEllipse(rect);
509
510 handlePathNode(node, p);
511}
512
513void QSvgVisitorImpl::visitPathNode(const QSvgPath *node)
514{
515 handlePathNode(node, node->path());
516}
517
518void QSvgVisitorImpl::visitLineNode(const QSvgLine *node)
519{
520 QPainterPath p;
521 p.moveTo(node->line().p1());
522 p.lineTo(node->line().p2());
523 handlePathNode(node, p);
524}
525
526void QSvgVisitorImpl::visitPolygonNode(const QSvgPolygon *node)
527{
528 QPainterPath p = QQuickVectorImageGenerator::Utils::polygonToPath(node->polygon(), true);
529 handlePathNode(node, p);
530}
531
532void QSvgVisitorImpl::visitPolylineNode(const QSvgPolyline *node)
533{
534 QPainterPath p = QQuickVectorImageGenerator::Utils::polygonToPath(node->polygon(), false);
535 handlePathNode(node, p);
536}
537
538QString QSvgVisitorImpl::gradientCssDescription(const QGradient *gradient)
539{
540 QString cssDescription;
541 if (gradient->type() == QGradient::LinearGradient) {
542 const QLinearGradient *linearGradient = static_cast<const QLinearGradient *>(gradient);
543
544 cssDescription += " -qt-foreground: qlineargradient("_L1;
545 cssDescription += "x1:"_L1 + QString::number(linearGradient->start().x()) + u',';
546 cssDescription += "y1:"_L1 + QString::number(linearGradient->start().y()) + u',';
547 cssDescription += "x2:"_L1 + QString::number(linearGradient->finalStop().x()) + u',';
548 cssDescription += "y2:"_L1 + QString::number(linearGradient->finalStop().y()) + u',';
549 } else if (gradient->type() == QGradient::RadialGradient) {
550 const QRadialGradient *radialGradient = static_cast<const QRadialGradient *>(gradient);
551
552 cssDescription += " -qt-foreground: qradialgradient("_L1;
553 cssDescription += "cx:"_L1 + QString::number(radialGradient->center().x()) + u',';
554 cssDescription += "cy:"_L1 + QString::number(radialGradient->center().y()) + u',';
555 cssDescription += "fx:"_L1 + QString::number(radialGradient->focalPoint().x()) + u',';
556 cssDescription += "fy:"_L1 + QString::number(radialGradient->focalPoint().y()) + u',';
557 cssDescription += "radius:"_L1 + QString::number(radialGradient->radius()) + u',';
558 } else {
559 const QConicalGradient *conicalGradient = static_cast<const QConicalGradient *>(gradient);
560
561 cssDescription += " -qt-foreground: qconicalgradient("_L1;
562 cssDescription += "cx:"_L1 + QString::number(conicalGradient->center().x()) + u',';
563 cssDescription += "cy:"_L1 + QString::number(conicalGradient->center().y()) + u',';
564 cssDescription += "angle:"_L1 + QString::number(conicalGradient->angle()) + u',';
565 }
566
567 const QStringList coordinateModes = { "logical"_L1, "stretchtodevice"_L1, "objectbounding"_L1, "object"_L1 };
568 cssDescription += "coordinatemode:"_L1;
569 cssDescription += coordinateModes.at(int(gradient->coordinateMode()));
570 cssDescription += u',';
571
572 const QStringList spreads = { "pad"_L1, "reflect"_L1, "repeat"_L1 };
573 cssDescription += "spread:"_L1;
574 cssDescription += spreads.at(int(gradient->spread()));
575
576 for (const QGradientStop &stop : gradient->stops()) {
577 cssDescription += ",stop:"_L1;
578 cssDescription += QString::number(stop.first);
579 cssDescription += u' ';
580 cssDescription += stop.second.name(QColor::HexArgb);
581 }
582
583 cssDescription += ");"_L1;
584
585 return cssDescription;
586}
587
588QString QSvgVisitorImpl::colorCssDescription(QColor color)
589{
590 QString cssDescription;
591 cssDescription += QStringLiteral("rgba(");
592 cssDescription += QString::number(color.red()) + QStringLiteral(",");
593 cssDescription += QString::number(color.green()) + QStringLiteral(",");
594 cssDescription += QString::number(color.blue()) + QStringLiteral(",");
595 cssDescription += QString::number(color.alphaF()) + QStringLiteral(")");
596
597 return cssDescription;
598}
599
600namespace {
601
602 // Simple class for representing the SVG font as a font engine
603 // We use the Proxy font engine type, which is currently unused and does not map to
604 // any specific font engine
605 // (The QSvgFont object must outlive the engine.)
606 class QSvgFontEngine : public QFontEngine
607 {
608 public:
609 QSvgFontEngine(const QSvgFont *font, qreal size);
610
611 QFontEngine *cloneWithSize(qreal size) const override;
612
613 glyph_t glyphIndex(uint ucs4) const override;
614 int stringToCMap(const QChar *str,
615 int len,
616 QGlyphLayout *glyphs,
617 int *nglyphs,
618 ShaperFlags flags) const override;
619
620 void addGlyphsToPath(glyph_t *glyphs,
621 QFixedPoint *positions,
622 int nGlyphs,
623 QPainterPath *path,
624 QTextItem::RenderFlags flags) override;
625
626 glyph_metrics_t boundingBox(glyph_t glyph) override;
627
628 void recalcAdvances(QGlyphLayout *, ShaperFlags) const override;
629 QFixed ascent() const override;
630 QFixed capHeight() const override;
631 QFixed descent() const override;
632 QFixed leading() const override;
633 qreal maxCharWidth() const override;
634 qreal minLeftBearing() const override;
635 qreal minRightBearing() const override;
636
637 QFixed emSquareSize() const override;
638
639 private:
640 const QSvgFont *m_font;
641 };
642
643 QSvgFontEngine::QSvgFontEngine(const QSvgFont *font, qreal size)
644 : QFontEngine(Proxy)
645 , m_font(font)
646 {
647 fontDef.pixelSize = size;
648 fontDef.families = QStringList(m_font->m_familyName);
649 }
650
651 QFixed QSvgFontEngine::emSquareSize() const
652 {
653 return QFixed::fromReal(m_font->m_unitsPerEm);
654 }
655
656 glyph_t QSvgFontEngine::glyphIndex(uint ucs4) const
657 {
658 const ushort c(ucs4);
659 if (ucs4 < USHRT_MAX && m_font->findFirstGlyphFor(QStringView(&c, 1)))
660 return glyph_t(ucs4);
661
662 return 0;
663 }
664
665 int QSvgFontEngine::stringToCMap(const QChar *str,
666 int len,
667 QGlyphLayout *glyphs,
668 int *nglyphs,
669 ShaperFlags flags) const
670 {
671 Q_ASSERT(glyphs->numGlyphs >= *nglyphs);
672 if (*nglyphs < len) {
673 *nglyphs = len;
674 return -1;
675 }
676
677 int ucs4Length = 0;
678 QStringIterator it(str, str + len);
679 while (it.hasNext()) {
680 char32_t ucs4 = it.next();
681 glyph_t index = glyphIndex(ucs4);
682 glyphs->glyphs[ucs4Length++] = index;
683 }
684
685 *nglyphs = ucs4Length;
686 glyphs->numGlyphs = ucs4Length;
687
688 if (!(flags & GlyphIndicesOnly))
689 recalcAdvances(glyphs, flags);
690
691 return *nglyphs;
692 }
693
694 void QSvgFontEngine::addGlyphsToPath(glyph_t *glyphs,
695 QFixedPoint *positions,
696 int nGlyphs,
697 QPainterPath *path,
698 QTextItem::RenderFlags flags)
699 {
700 Q_UNUSED(flags);
701 const qreal scale = fontDef.pixelSize / m_font->m_unitsPerEm;
702 for (int i = 0; i < nGlyphs; ++i) {
703 glyph_t index = glyphs[i];
704 if (index > 0) {
705 QPointF position = positions[i].toPointF();
706 const ushort c(index);
707 const QSvgGlyph *foundGlyph = m_font->findFirstGlyphFor(QStringView(&c, 1));
708
709 if (!foundGlyph)
710 continue;
711
712 QPainterPath glyphPath = foundGlyph->m_path;
713
714 QTransform xform;
715 xform.translate(position.x(), position.y());
716 xform.scale(scale, -scale);
717 glyphPath = xform.map(glyphPath);
718 path->addPath(glyphPath);
719 }
720 }
721 }
722
723 glyph_metrics_t QSvgFontEngine::boundingBox(glyph_t glyph)
724 {
725 glyph_metrics_t ret;
726 ret.x = 0; // left bearing
727 ret.y = -ascent();
728 const qreal scale = fontDef.pixelSize / m_font->m_unitsPerEm;
729 const ushort c(glyph);
730 const QSvgGlyph *svgGlyph = m_font->findFirstGlyphFor(QStringView(&c, 1));
731 ret.width = QFixed::fromReal(svgGlyph ? svgGlyph->m_horizAdvX * scale : 0.);
732 ret.height = ascent() + descent();
733 return ret;
734 }
735
736 QFontEngine *QSvgFontEngine::cloneWithSize(qreal size) const
737 {
738 QSvgFontEngine *otherEngine = new QSvgFontEngine(m_font, size);
739 return otherEngine;
740 }
741
742 void QSvgFontEngine::recalcAdvances(QGlyphLayout *glyphLayout, ShaperFlags) const
743 {
744 const qreal scale = fontDef.pixelSize / m_font->m_unitsPerEm;
745 for (int i = 0; i < glyphLayout->numGlyphs; i++) {
746 const ushort c(glyphLayout->glyphs[i]);
747 const QSvgGlyph *svgGl = m_font->findFirstGlyphFor(QStringView(&c, 1));
748 glyphLayout->advances[i] = QFixed::fromReal(svgGl ? svgGl->m_horizAdvX * scale : 0.);
749 }
750 }
751
752 QFixed QSvgFontEngine::ascent() const
753 {
754 return QFixed::fromReal(fontDef.pixelSize);
755 }
756
757 QFixed QSvgFontEngine::capHeight() const
758 {
759 return ascent();
760 }
761 QFixed QSvgFontEngine::descent() const
762 {
763 return QFixed{};
764 }
765
766 QFixed QSvgFontEngine::leading() const
767 {
768 return QFixed{};
769 }
770
771 qreal QSvgFontEngine::maxCharWidth() const
772 {
773 const qreal scale = fontDef.pixelSize / m_font->m_unitsPerEm;
774 return m_font->m_horizAdvX * scale;
775 }
776
777 qreal QSvgFontEngine::minLeftBearing() const
778 {
779 return 0.0;
780 }
781
782 qreal QSvgFontEngine::minRightBearing() const
783 {
784 return 0.0;
785 }
786}
787
788static QVariant calculateInterpolatedValue(const QSvgAbstractAnimatedProperty *property, int index, int)
789{
790 if (index == 0)
791 const_cast<QSvgAbstractAnimatedProperty *>(property)->interpolate(1, 0.0);
792 else
793 const_cast<QSvgAbstractAnimatedProperty *>(property)->interpolate(index, 1.0);
794
795 return property->interpolatedValue();
796}
797
798void QSvgVisitorImpl::visitTextNode(const QSvgText *node)
799{
800 handleBaseNodeSetup(node);
801 const bool isTextArea = node->type() == QSvgNode::Textarea;
802
803 QString text;
804 const QSvgFont *svgFont = m_styleResolver->states().svgFont;
805 bool needsRichText = false;
806 bool preserveWhiteSpace = node->whitespaceMode() == QSvgText::Preserve;
807 const QGradient *mainGradient = m_styleResolver->currentFillGradient();
808
809 QFontEngine *fontEngine = nullptr;
810 if (svgFont != nullptr) {
811 fontEngine = new QSvgFontEngine(svgFont, m_styleResolver->painter().font().pointSize());
812 fontEngine->ref.ref();
813 }
814
815#if QT_CONFIG(texthtmlparser)
816 bool needsPathNode = mainGradient != nullptr
817 || svgFont != nullptr
818 || m_styleResolver->currentStrokeGradient() != nullptr;
819#endif
820 for (const auto *tspan : node->tspans()) {
821 if (!tspan) {
822 text += QStringLiteral("<br>");
823 continue;
824 }
825
826 // Note: We cannot get the font directly from the style, since this does
827 // not apply the weight, since this is relative and depends on current state.
828 handleBaseNodeSetup(tspan);
829 QFont font = m_styleResolver->painter().font();
830
831 QString styleTagContent;
832
833 if ((font.resolveMask() & QFont::FamilyResolved)
834 || (font.resolveMask() & QFont::FamiliesResolved)) {
835 styleTagContent += QStringLiteral("font-family: %1;").arg(font.family());
836 }
837
838 if (font.resolveMask() & QFont::WeightResolved
839 && font.weight() != QFont::Normal
840 && font.weight() != QFont::Bold) {
841 styleTagContent += QStringLiteral("font-weight: %1;").arg(int(font.weight()));
842 }
843
844 if (font.resolveMask() & QFont::SizeResolved) {
845 // Pixel size stored as point size in SVG parser
846 styleTagContent += QStringLiteral("font-size: %1px;").arg(int(font.pointSizeF()));
847 }
848
849 if (font.resolveMask() & QFont::CapitalizationResolved
850 && font.capitalization() == QFont::SmallCaps) {
851 styleTagContent += QStringLiteral("font-variant: small-caps;");
852 }
853
854 if (m_styleResolver->currentFillGradient() != nullptr
855 && m_styleResolver->currentFillGradient() != mainGradient) {
856 const QGradient grad = m_styleResolver->applyOpacityToGradient(*m_styleResolver->currentFillGradient(), m_styleResolver->currentFillOpacity());
857 styleTagContent += gradientCssDescription(&grad) + u';';
858#if QT_CONFIG(texthtmlparser)
859 needsPathNode = true;
860#endif
861 }
862
863 const QColor currentStrokeColor = m_styleResolver->currentStrokeColor();
864 if (currentStrokeColor.alpha() > 0) {
865 QString strokeColor = colorCssDescription(currentStrokeColor);
866 styleTagContent += QStringLiteral("-qt-stroke-color:%1;").arg(strokeColor);
867 styleTagContent += QStringLiteral("-qt-stroke-width:%1px;").arg(m_styleResolver->currentStrokeWidth());
868 styleTagContent += QStringLiteral("-qt-stroke-dasharray:%1;").arg(dashArrayString(m_styleResolver->currentStroke().dashPattern()));
869 styleTagContent += QStringLiteral("-qt-stroke-dashoffset:%1;").arg(m_styleResolver->currentStroke().dashOffset());
870 styleTagContent += QStringLiteral("-qt-stroke-lineCap:%1;").arg(capStyleName(m_styleResolver->currentStroke().capStyle()));
871 styleTagContent += QStringLiteral("-qt-stroke-lineJoin:%1;").arg(joinStyleName(m_styleResolver->currentStroke().joinStyle()));
872 if (m_styleResolver->currentStroke().joinStyle() == Qt::MiterJoin || m_styleResolver->currentStroke().joinStyle() == Qt::SvgMiterJoin)
873 styleTagContent += QStringLiteral("-qt-stroke-miterlimit:%1;").arg(m_styleResolver->currentStroke().miterLimit());
874#if QT_CONFIG(texthtmlparser)
875 needsPathNode = true;
876#endif
877 }
878
879 if (tspan->whitespaceMode() == QSvgText::Preserve && !preserveWhiteSpace)
880 styleTagContent += QStringLiteral("white-space: pre-wrap;");
881
882 QString content = tspan->text().toHtmlEscaped();
883 content.replace(QLatin1Char('\t'), QLatin1Char(' '));
884 content.replace(QLatin1Char('\n'), QLatin1Char(' '));
885
886 bool fontTag = false;
887 if (!tspan->style().fill.isDefault()) {
888 auto &b = tspan->style().fill->qbrush();
889 qCDebug(lcQuickVectorImage) << "tspan FILL:" << b;
890 if (b.style() != Qt::NoBrush)
891 {
892 if (qFuzzyCompare(b.color().alphaF() + 1.0, 2.0))
893 {
894 QString spanColor = b.color().name();
895 fontTag = !spanColor.isEmpty();
896 if (fontTag)
897 text += QStringLiteral("<font color=\"%1\">").arg(spanColor);
898 } else {
899 QString spanColor = colorCssDescription(b.color());
900 styleTagContent += QStringLiteral("color:%1").arg(spanColor);
901 }
902 }
903 }
904
905 needsRichText = needsRichText || !styleTagContent.isEmpty();
906 if (!styleTagContent.isEmpty())
907 text += QStringLiteral("<span style=\"%1\">").arg(styleTagContent.toHtmlEscaped());
908
909 if (font.resolveMask() & QFont::WeightResolved && font.bold())
910 text += QStringLiteral("<b>");
911
912 if (font.resolveMask() & QFont::StyleResolved && font.italic())
913 text += QStringLiteral("<i>");
914
915 if (font.resolveMask() & QFont::CapitalizationResolved) {
916 switch (font.capitalization()) {
917 case QFont::AllLowercase:
918 content = content.toLower();
919 break;
920 case QFont::AllUppercase:
921 content = content.toUpper();
922 break;
923 case QFont::Capitalize:
924 // ### We need to iterate over the string and do the title case conversion,
925 // since this is not part of QString.
926 qCWarning(lcQuickVectorImage) << "Title case not implemented for tspan";
927 break;
928 default:
929 break;
930 }
931 }
932 text += content;
933 if (fontTag)
934 text += QStringLiteral("</font>");
935
936 if (font.resolveMask() & QFont::StyleResolved && font.italic())
937 text += QStringLiteral("</i>");
938
939 if (font.resolveMask() & QFont::WeightResolved && font.bold())
940 text += QStringLiteral("</b>");
941
942 if (!styleTagContent.isEmpty())
943 text += QStringLiteral("</span>");
944
945 handleBaseNodeEnd(tspan);
946 }
947
948 if (preserveWhiteSpace && (needsRichText || m_styleResolver->currentFillGradient() != nullptr))
949 text = QStringLiteral("<span style=\"white-space: pre-wrap\">") + text + QStringLiteral("</span>");
950
951 QFont font = m_styleResolver->painter().font();
952 if (font.pixelSize() <= 0 && font.pointSize() > 0)
953 font.setPixelSize(font.pointSize()); // Pixel size stored as point size by SVG parser
954
955 font.setHintingPreference(QFont::PreferNoHinting);
956
957#if QT_CONFIG(texthtmlparser)
958 if (needsPathNode) {
959 QTextDocument document;
960 document.setHtml(text);
961 if (isTextArea && node->size().width() > 0)
962 document.setTextWidth(node->size().width());
963 document.setDefaultFont(font);
964 document.pageCount(); // Force layout
965
966 QTextBlock block = document.firstBlock();
967 while (block.isValid()) {
968 QTextLayout *lout = block.layout();
969
970 if (lout != nullptr) {
971 QRectF boundingRect = lout->boundingRect();
972
973 // If this block has requested the current SVG font, we override it
974 // (note that this limits the text to one svg font, but this is also the case
975 // in the QPainter at the moment, and needs a more centralized solution in Qt Svg
976 // first)
977 QFont blockFont = block.charFormat().font();
978 if (svgFont != nullptr
979 && blockFont.family() == svgFont->m_familyName) {
980 QRawFont rawFont;
981 QRawFontPrivate *rawFontD = QRawFontPrivate::get(rawFont);
982 rawFontD->setFontEngine(fontEngine->cloneWithSize(blockFont.pixelSize()));
983
984 lout->setRawFont(rawFont);
985 }
986
987 auto addPathForFormat = [&](QPainterPath p, QTextCharFormat fmt, int pathIndex) {
988 PathNodeInfo info;
989 fillCommonNodeInfo(node, info, QStringLiteral("_path%1").arg(pathIndex));
990 fillPathAnimationInfo(node, info);
991 auto fillStyle = node->style().fill;
992 if (fillStyle)
993 info.fillRule = fillStyle->fillRule();
994
995 if (fmt.hasProperty(QTextCharFormat::ForegroundBrush)) {
996 info.fillColor.setDefaultValue(fmt.foreground().color());
997 if (fmt.foreground().gradient() != nullptr && fmt.foreground().gradient()->type() != QGradient::NoGradient)
998 info.grad = *fmt.foreground().gradient();
999 } else {
1000 info.fillColor.setDefaultValue(m_styleResolver->currentFillColor());
1001 }
1002
1003 info.path.setDefaultValue(QVariant::fromValue(p));
1004
1005 const QGradient *strokeGradient = m_styleResolver->currentStrokeGradient();
1006 QPen pen;
1007 if (fmt.hasProperty(QTextCharFormat::TextOutline)) {
1008 pen = fmt.textOutline();
1009 if (strokeGradient == nullptr) {
1010 info.strokeStyle = StrokeStyle::fromPen(pen);
1011 info.strokeStyle.color.setDefaultValue(pen.color());
1012 }
1013 } else {
1014 pen = m_styleResolver->currentStroke();
1015 if (strokeGradient == nullptr) {
1016 info.strokeStyle = StrokeStyle::fromPen(pen);
1017 info.strokeStyle.color.setDefaultValue(m_styleResolver->currentStrokeColor());
1018 }
1019 }
1020
1021 if (info.grad.type() == QGradient::NoGradient && m_styleResolver->currentFillGradient() != nullptr)
1022 info.grad = m_styleResolver->applyOpacityToGradient(*m_styleResolver->currentFillGradient(), m_styleResolver->currentFillOpacity());
1023
1024 info.fillTransform = m_styleResolver->currentFillTransform();
1025
1026 m_generator->generatePath(info, boundingRect);
1027
1028 if (strokeGradient != nullptr) {
1029 PathNodeInfo strokeInfo;
1030 fillCommonNodeInfo(node, strokeInfo, QStringLiteral("_stroke%1").arg(pathIndex));
1031 fillPathAnimationInfo(node, strokeInfo);
1032
1033 strokeInfo.grad = *strokeGradient;
1034
1035 QPainterPathStroker stroker(pen);
1036 strokeInfo.path.setDefaultValue(QVariant::fromValue(stroker.createStroke(p)));
1037 m_generator->generatePath(strokeInfo, boundingRect);
1038 }
1039 };
1040
1041 qreal baselineOffset = -QFontMetricsF(font).ascent();
1042 if (lout->lineCount() > 0 && lout->lineAt(0).isValid())
1043 baselineOffset = -lout->lineAt(0).ascent();
1044
1045 const QPointF baselineTranslation(0.0, baselineOffset);
1046 auto glyphsToPath = [&](QList<QGlyphRun> glyphRuns, qreal width) {
1047 QList<QPainterPath> paths;
1048 for (const QGlyphRun &glyphRun : glyphRuns) {
1049 QRawFont font = glyphRun.rawFont();
1050 QList<quint32> glyphIndexes = glyphRun.glyphIndexes();
1051 QList<QPointF> positions = glyphRun.positions();
1052
1053 for (qsizetype j = 0; j < glyphIndexes.size(); ++j) {
1054 quint32 glyphIndex = glyphIndexes.at(j);
1055 const QPointF &pos = positions.at(j);
1056
1057 QPainterPath p = font.pathForGlyph(glyphIndex);
1058 p.translate(pos + node->position() + baselineTranslation);
1059 if (m_styleResolver->states().textAnchor == Qt::AlignHCenter)
1060 p.translate(QPointF(-0.5 * width, 0));
1061 else if (m_styleResolver->states().textAnchor == Qt::AlignRight)
1062 p.translate(QPointF(-width, 0));
1063 paths.append(p);
1064 }
1065 }
1066
1067 return paths;
1068 };
1069
1070 QList<QTextLayout::FormatRange> formats = block.textFormats();
1071 for (int i = 0; i < formats.size(); ++i) {
1072 QTextLayout::FormatRange range = formats.at(i);
1073
1074 QList<QGlyphRun> glyphRuns = lout->glyphRuns(range.start, range.length);
1075 QList<QPainterPath> paths = glyphsToPath(glyphRuns, lout->minimumWidth());
1076 for (int j = 0; j < paths.size(); ++j) {
1077 const QPainterPath &path = paths.at(j);
1078 addPathForFormat(path, range.format, j);
1079 }
1080 }
1081 }
1082
1083 block = block.next();
1084 }
1085 } else
1086#endif
1087 {
1088 TextNodeInfo info;
1089 fillCommonNodeInfo(node, info);
1090 fillAnimationInfo(node, info);
1091
1092 {
1093 QList<AnimationPair> animations = collectAnimations(node, QStringLiteral("fill"));
1094 if (!animations.isEmpty())
1095 applyAnimationsToProperty(animations, &info.fillColor, calculateInterpolatedValue);
1096 }
1097
1098 {
1099 QList<AnimationPair> animations = collectAnimations(node, QStringLiteral("fill-opacity"));
1100 if (!animations.isEmpty())
1101 applyAnimationsToProperty(animations, &info.fillOpacity, calculateInterpolatedValue);
1102 }
1103
1104 {
1105 QList<AnimationPair> animations = collectAnimations(node, QStringLiteral("stroke"));
1106 if (!animations.isEmpty())
1107 applyAnimationsToProperty(animations, &info.strokeColor, calculateInterpolatedValue);
1108 }
1109
1110 {
1111 QList<AnimationPair> animations = collectAnimations(node, QStringLiteral("stroke-opacity"));
1112 if (!animations.isEmpty())
1113 applyAnimationsToProperty(animations, &info.strokeOpacity, calculateInterpolatedValue);
1114 }
1115
1116 info.position = node->position();
1117 info.size = node->size();
1118 info.font = font;
1119 info.text = text;
1120 info.isTextArea = isTextArea;
1121 info.needsRichText = needsRichText;
1122 info.fillColor.setDefaultValue(m_styleResolver->currentFillColor());
1123 info.alignment = m_styleResolver->states().textAnchor;
1124 info.strokeColor.setDefaultValue(m_styleResolver->currentStrokeColor());
1125
1126 m_generator->generateTextNode(info);
1127 }
1128
1129 handleBaseNodeEnd(node);
1130
1131 if (fontEngine != nullptr) {
1132 fontEngine->ref.deref();
1133 Q_ASSERT(fontEngine->ref.loadRelaxed() == 0);
1134 delete fontEngine;
1135 }
1136}
1137
1138void QSvgVisitorImpl::visitUseNode(const QSvgUse *node)
1139{
1140 QSvgNode *link = node->link();
1141 if (!link)
1142 return;
1143 handleBaseNodeSetup(node);
1144 UseNodeInfo info;
1145 QPointF startPos = node->start();
1146 fillCommonNodeInfo(node, info);
1147 fillAnimationInfo(node, info);
1148 if (!info.bounds.isNull())
1149 info.bounds.translate(-startPos);
1151 if (!startPos.isNull()) {
1152 QTransform xform;
1153 if (!info.isDefaultTransform)
1154 xform = info.transform.defaultValue().value<QTransform>();
1155 xform.translate(startPos.x(), startPos.y());
1156 info.transform.setDefaultValue(QVariant::fromValue(xform));
1157 info.isDefaultTransform = false;
1158 }
1159 m_generator->generateUseNode(info);
1160 QString oldLinkSuffix = m_linkSuffix;
1161 m_linkSuffix += QStringLiteral("_use") + info.id;
1162 m_useLevel++;
1163 QSvgVisitor::traverse(link);
1164 m_useLevel--;
1165 m_linkSuffix = oldLinkSuffix;
1167 m_generator->generateUseNode(info);
1168 handleBaseNodeEnd(node);
1169}
1170
1171bool QSvgVisitorImpl::visitSwitchNodeStart(const QSvgSwitch *node)
1172{
1173 QSvgNode *link = node->childToRender();
1174 if (!link)
1175 return false;
1176
1177 QString oldLinkSuffix = m_linkSuffix;
1178 m_linkSuffix += QStringLiteral("_switch") + QString::number(quintptr(node), 16);
1179 QSvgVisitor::traverse(link);
1180 m_linkSuffix = oldLinkSuffix;
1181
1182 return false;
1183}
1184
1185void QSvgVisitorImpl::visitSwitchNodeEnd(const QSvgSwitch *node)
1186{
1187 Q_UNUSED(node);
1188}
1189
1190bool QSvgVisitorImpl::visitDefsNodeStart(const QSvgDefs *node)
1191{
1192 Q_UNUSED(node);
1193 return m_pregeneratingReferencedNodes;
1194}
1195
1196void QSvgVisitorImpl::visitDefsNodeEnd(const QSvgDefs *node)
1197{
1198 Q_UNUSED(node);
1199}
1200
1201bool QSvgVisitorImpl::visitPatternNodeStart(const QSvgPattern *node)
1202{
1203 if (m_pregeneratingReferencedNodes) {
1204 handleBaseNodeSetup(node);
1205
1206 PatternNodeInfo info;
1207 fillCommonNodeInfo(node, info);
1208 fillAnimationInfo(node, info);
1209
1210 info.stage = StructureNodeStage::Start;
1211
1212 QSvgRectF r = node->rect();
1213 info.isPatternRectRelativeCoordinates = r.unitX() == QtSvg::UnitTypes::objectBoundingBox;
1214 info.patternRect = r;
1215
1216 if (node->contentUnits() == QtSvg::UnitTypes::objectBoundingBox)
1217 qCWarning(lcQuickVectorImage) << "Only user space content units supported for patterns";
1218
1219 return m_generator->generatePatternNode(info);
1220 } else {
1221 return false;
1222 }
1223}
1224
1225void QSvgVisitorImpl::visitPatternNodeEnd(const QSvgPattern *node)
1226{
1227 Q_ASSERT(m_pregeneratingReferencedNodes);
1228
1229 handleBaseNodeSetup(node);
1230
1231 PatternNodeInfo info;
1232 fillCommonNodeInfo(node, info);
1233 fillAnimationInfo(node, info);
1234
1235 QSvgRectF r = node->rect();
1236 info.isPatternRectRelativeCoordinates = r.unitX() == QtSvg::UnitTypes::objectBoundingBox;
1237 info.patternRect = r;
1238
1239 info.stage = StructureNodeStage::End;
1240
1241 m_generator->generatePatternNode(info);
1242}
1243
1244bool QSvgVisitorImpl::visitSymbolNodeStart(const QSvgSymbol *node)
1245{
1246 if (m_useLevel == 0)
1247 return false;
1248
1249 handleBaseNodeSetup(node);
1250
1251 StructureNodeInfo info;
1252 fillCommonNodeInfo(node, info);
1253 fillAnimationInfo(node, info);
1254
1255 QTransform oldTransform = info.transform.defaultValue().value<QTransform>();
1256 info.clipBox = oldTransform.mapRect(node->clipRect());
1257
1258 QTransform xform = node->aspectRatioTransform();
1259 if (!xform.isIdentity()) {
1260 info.isDefaultTransform = false;
1261 xform = xform * oldTransform;
1262 info.transform.setDefaultValue(QVariant::fromValue(xform));
1263 }
1265
1266 return m_generator->generateStructureNode(info);
1267}
1268
1269void QSvgVisitorImpl::visitSymbolNodeEnd(const QSvgSymbol *node)
1270{
1271 handleBaseNodeSetup(node);
1272
1273 StructureNodeInfo info;
1274 fillCommonNodeInfo(node, info);
1275 fillAnimationInfo(node, info);
1276
1277 info.clipBox = node->clipRect();
1279
1280 m_generator->generateStructureNode(info);
1281}
1282
1283bool QSvgVisitorImpl::visitMaskNodeStart(const QSvgMask *node)
1284{
1285 if (!m_pregeneratingReferencedNodes)
1286 return false;
1287
1288 handleBaseNodeSetup(node);
1289
1290 MaskNodeInfo info;
1291
1292 QSvgRectF r = node->rect();
1293 info.isMaskRectRelativeCoordinates = r.unitX() == QtSvg::UnitTypes::objectBoundingBox;
1294 info.maskRect = r;
1295
1296 if (node->contentUnits() == QtSvg::UnitTypes::objectBoundingBox)
1297 qCWarning(lcQuickVectorImage) << "Only user space content units supported for masks";
1298
1299 fillCommonNodeInfo(node, info);
1300
1301 return m_generator->generateMaskNode(info);
1302}
1303
1304void QSvgVisitorImpl::visitMaskNodeEnd(const QSvgMask *node)
1305{
1306 MaskNodeInfo info;
1308
1309 QSvgRectF r = node->rect();
1310 info.isMaskRectRelativeCoordinates = r.unitX() == QtSvg::UnitTypes::objectBoundingBox;
1311 info.maskRect = r;
1312 fillCommonNodeInfo(node, info);
1313
1314 m_generator->generateMaskNode(info);
1315
1316 handleBaseNodeEnd(node);
1317}
1318
1319bool QSvgVisitorImpl::visitFilterNodeStart(const QSvgFilterContainer *node)
1320{
1321 Q_UNUSED(node)
1322
1323 if (!m_pregeneratingReferencedNodes)
1324 return false;
1325
1326 if (!m_filterPrimitives.isEmpty()) {
1327 qCWarning(lcQuickVectorImage) << "Filter defined inside a filter";
1328 return false;
1329 }
1330
1331 return true;
1332}
1333
1334void QSvgVisitorImpl::visitFilterNodeEnd(const QSvgFilterContainer *node)
1335{
1336 if (m_filterPrimitives.isEmpty())
1337 return;
1338
1339 handleBaseNodeSetup(node);
1340
1341 FilterNodeInfo info;
1342 fillCommonNodeInfo(node, info);
1343
1344 info.filterRect = node->rect();
1345 if (node->filterUnits() == QtSvg::UnitTypes::objectBoundingBox)
1347
1348 bool generatedAlpha = false;
1349 for (const QSvgFeFilterPrimitive *filterPrimitive : std::as_const(m_filterPrimitives)) {
1350 if (filterPrimitive->requiresSourceAlpha() && !generatedAlpha) {
1351 FilterNodeInfo::FilterStep alphaStep;
1352 alphaStep.filterType = FilterNodeInfo::Type::ColorMatrix;
1353 alphaStep.csFilterParameter = FilterNodeInfo::CoordinateSystem::MatchFilterRect;
1354
1355 // Isolate alpha
1356 qreal values[] = { 0.0, 0.0, 0.0, 0.0, 0.0,
1357 0.0, 0.0, 0.0, 0.0, 0.0,
1358 0.0, 0.0, 0.0, 0.0, 0.0,
1359 0.0, 0.0, 0.0, 1.0, 0.0,
1360 0.0, 0.0, 0.0, 0.0, 0.0 };
1361 QGenericMatrix<5, 5, qreal> matrix(values);
1362 alphaStep.filterParameter = QVariant::fromValue(matrix);
1363 alphaStep.outputName = info.id + QStringLiteral("_source_alpha");
1364 generatedAlpha = true;
1365
1366 info.steps.append(alphaStep);
1367 }
1368
1369 fillFilterPrimitiveInfo(node, filterPrimitive, info);
1370 }
1371
1372 m_generator->generateFilterNode(info);
1373 m_filterPrimitives.clear();
1374}
1375
1376void QSvgVisitorImpl::fillFilterPrimitiveInfo(const QSvgFilterContainer *node,
1377 const QSvgFeFilterPrimitive *filterPrimitive,
1378 FilterNodeInfo &info)
1379{
1381 step.filterPrimitiveRect = filterPrimitive->rect();
1382
1383 step.outputName = info.id + QStringLiteral("_")
1384 + (filterPrimitive->result().isEmpty()
1385 ? QStringLiteral("output_") + QString::number(info.steps.size())
1386 : filterPrimitive->result());
1387
1388 auto findInput = [&info, &filterPrimitive](const QString &input, QString *outName) {
1389 const QString alphaSource = info.id + QStringLiteral("_source_alpha");
1390 if (input == QStringLiteral("SourceGraphic")) {
1392 } else if (input == QStringLiteral("SourceAlpha")) {
1393 *outName = alphaSource;
1395 } else if (info.steps.isEmpty()) {
1397 }
1398
1399 if (!input.isEmpty()) {
1400 *outName = info.id + QStringLiteral("_") + input;
1401 } else {
1402 bool insideMergeNode = filterPrimitive->type() == QSvgNode::FeMergenode;
1403 for (int i = info.steps.size() - 1; i >= 0; --i) {
1404 const auto &prevStep = info.steps.at(i);
1405 if (insideMergeNode && prevStep.filterType == FilterNodeInfo::Type::Merge) {
1406 insideMergeNode = false;
1407 continue;
1408 }
1409
1410 if (!prevStep.outputName.isEmpty() && prevStep.outputName != alphaSource) {
1411 *outName = prevStep.outputName;
1412 break;
1413 }
1414 }
1415 }
1416
1418 };
1419
1420 step.input1 = findInput(filterPrimitive->input(), &step.namedInput1);
1421
1422 if (node->primitiveUnits() == QtSvg::UnitTypes::objectBoundingBox)
1424
1425 // We special-case the default filter primitive rect as is done in Qt Svg.
1426 // The default is to match the filter's rect and this is represented by making
1427 // the types of the filter primitive's rect QtSvg::UnitTypes::unknown. Since
1428 // this is not generally handled in Qt Svg, we also just special case it here.
1429 if (node->primitiveUnits() == QtSvg::UnitTypes::userSpaceOnUse
1430 && filterPrimitive->rect().unitW() == QtSvg::UnitTypes::unknown) {
1432 }
1433
1434 switch (filterPrimitive->type()) {
1435 case QSvgNode::FeMerge:
1437 break;
1438 case QSvgNode::FeMergenode:
1440 break;
1441 case QSvgNode::FeBlend:
1442 {
1443 const QSvgFeBlend *blend = static_cast<const QSvgFeBlend *>(filterPrimitive);
1444 switch (blend->mode()) {
1445 case QSvgFeBlend::Mode::Normal:
1447 break;
1448 case QSvgFeBlend::Mode::Multiply:
1450 break;
1451 case QSvgFeBlend::Mode::Screen:
1453 break;
1454 case QSvgFeBlend::Mode::Darken:
1456 break;
1457 case QSvgFeBlend::Mode::Lighten:
1459 break;
1460 }
1461
1462 step.input2 = findInput(blend->input2(), &step.namedInput2);
1463 break;
1464 }
1465 case QSvgNode::FeComposite:
1466 {
1467 const QSvgFeComposite *composite = static_cast<const QSvgFeComposite *>(filterPrimitive);
1468 switch (composite->compositionOperator()) {
1469 case QSvgFeComposite::Operator::Over:
1471 break;
1472 case QSvgFeComposite::Operator::In:
1474 break;
1475 case QSvgFeComposite::Operator::Out:
1477 break;
1478 case QSvgFeComposite::Operator::Atop:
1480 break;
1481 case QSvgFeComposite::Operator::Xor:
1483 break;
1484 case QSvgFeComposite::Operator::Lighter:
1486 break;
1487 case QSvgFeComposite::Operator::Arithmetic:
1489 break;
1490 };
1491
1492 step.input2 = findInput(composite->input2(), &step.namedInput2);
1493 step.filterParameter = composite->k();
1494 break;
1495 }
1496 case QSvgNode::FeOffset:
1497 {
1498 const QSvgFeOffset *offset = static_cast<const QSvgFeOffset *>(filterPrimitive);
1500 step.filterParameter = QVariant::fromValue(QVector2D(offset->dx(), offset->dy()));
1501 break;
1502
1503 }
1504 case QSvgNode::FeColormatrix:
1505 {
1506 const QSvgFeColorMatrix *colorMatrix =
1507 static_cast<const QSvgFeColorMatrix *>(filterPrimitive);
1509 step.filterParameter = QVariant::fromValue(colorMatrix->matrix());
1510 break;
1511 }
1512
1513 case QSvgNode::FeGaussianblur:
1514 {
1515 const QSvgFeGaussianBlur *gaussianBlur =
1516 static_cast<const QSvgFeGaussianBlur *>(filterPrimitive);
1517
1518 if (gaussianBlur->edgeMode() == QSvgFeGaussianBlur::EdgeMode::Wrap)
1519 info.wrapMode = QSGTexture::Repeat;
1521 if (!qFuzzyCompare(gaussianBlur->stdDeviationX(), gaussianBlur->stdDeviationY()))
1522 qCWarning(lcQuickVectorImage) << "Separate X and Y deviations not supported for gaussian blur";
1523 step.filterParameter = std::max(gaussianBlur->stdDeviationX(),
1524 gaussianBlur->stdDeviationY());
1525 break;
1526 }
1527
1528 case QSvgNode::FeFlood:
1529 {
1530 const QSvgFeFlood *flood =
1531 static_cast<const QSvgFeFlood *>(filterPrimitive);
1532
1534 step.filterParameter = flood->color();
1535 break;
1536 }
1537 default:
1538 // Create a dummy filter node to make sure bindings still work for unsupported filters
1540 break;
1541 }
1542
1543 info.steps.append(step);
1544}
1545
1546bool QSvgVisitorImpl::visitFeFilterPrimitiveNodeStart(const QSvgFeFilterPrimitive *node)
1547{
1548 m_filterPrimitives.append(node);
1549 return true;
1550}
1551
1552void QSvgVisitorImpl::visitFeFilterPrimitiveNodeEnd(const QSvgFeFilterPrimitive *node)
1553{
1554 Q_UNUSED(node);
1555}
1556
1557bool QSvgVisitorImpl::visitStructureNodeStart(const QSvgStructureNode *node)
1558{
1559 constexpr bool forceSeparatePaths = false;
1560 handleBaseNodeSetup(node);
1561
1562 StructureNodeInfo info;
1563
1564 fillCommonNodeInfo(node, info);
1565 fillAnimationInfo(node, info);
1566 info.forceSeparatePaths = forceSeparatePaths;
1567 info.isPathContainer = isPathContainer(node);
1569
1570 return m_generator->generateStructureNode(info);
1571}
1572
1573void QSvgVisitorImpl::visitStructureNodeEnd(const QSvgStructureNode *node)
1574{
1575 handleBaseNodeEnd(node);
1576 // qCDebug(lcQuickVectorGraphics) << "REVERT" << node->nodeId() << node->type() << (m_styleResolver->painter().pen().style() != Qt::NoPen) << m_styleResolver->painter().pen().color().name()
1577 // << (m_styleResolver->painter().pen().brush().style() != Qt::NoBrush) << m_styleResolver->painter().pen().brush().color().name();
1578
1579 StructureNodeInfo info;
1580 fillCommonNodeInfo(node, info);
1581 info.isPathContainer = isPathContainer(node);
1583
1584 m_generator->generateStructureNode(info);
1585}
1586
1587QString QSvgVisitorImpl::nextNodeId() const
1588{
1589 return QStringLiteral("_qt_node%1").arg(m_nodeIdCounter++);
1590}
1591
1592bool QSvgVisitorImpl::visitDocumentNodeStart(const QSvgDocument *node)
1593{
1594 handleBaseNodeSetup(node);
1595
1596 StructureNodeInfo info;
1597 fillCommonNodeInfo(node, info);
1598 fillAnimationInfo(node, info);
1599
1600 const QSvgDocument *doc = static_cast<const QSvgDocument *>(node);
1601 info.size = doc->size();
1602 info.viewBox = doc->viewBox();
1603 info.isPathContainer = isPathContainer(node);
1604 info.forceSeparatePaths = false;
1606
1607 if (m_generator->generateRootNode(info)) {
1608 pregenerateReferencedNodes(node);
1609 return true;
1610 } else {
1611 return false;
1612 }
1613}
1614
1615void QSvgVisitorImpl::visitDocumentNodeEnd(const QSvgDocument *node)
1616{
1617 handleBaseNodeEnd(node);
1618 qCDebug(lcQuickVectorImage) << "REVERT" << node->nodeId() << node->type() << (m_styleResolver->painter().pen().style() != Qt::NoPen)
1619 << m_styleResolver->painter().pen().color().name() << (m_styleResolver->painter().pen().brush().style() != Qt::NoBrush)
1620 << m_styleResolver->painter().pen().brush().color().name();
1621
1622 StructureNodeInfo info;
1623 fillCommonNodeInfo(node, info);
1625
1626 m_generator->generateRootNode(info);
1627}
1628
1629QString QSvgVisitorImpl::findOrCreateId(const QString &id)
1630{
1631 QString ret = m_idForNodeId.value(id);
1632 if (ret.isEmpty()) {
1633 ret = nextNodeId();
1634 m_idForNodeId.insert(id, ret);
1635 }
1636 return ret;
1637}
1638
1639QString QSvgVisitorImpl::findOrCreateId(const QSvgNode *node, const QString &nodeId)
1640{
1641 QString key = nodeId;
1642 const QSvgNode *n = m_nodesForKeys.value(key);
1643 if (key.isEmpty() || (n != nullptr && n != node))
1644 key = QString::number(quintptr(node), 16);
1645
1646 m_nodesForKeys.insert(key, node);
1647 return findOrCreateId(key);
1648}
1649
1650void QSvgVisitorImpl::fillCommonNodeInfo(const QSvgNode *node, NodeInfo &info, const QString &idSuffix)
1651{
1652 const QString nodeId = scrub(node->nodeId());
1653 info.id = findOrCreateId(node, nodeId);
1654
1655 // Internal disambiguation when multiple items come from the same node
1656 info.id += idSuffix;
1657
1658 if (!m_linkSuffix.isEmpty())
1659 info.id += m_linkSuffix;
1660
1661 info.nodeId = nodeId;
1662 info.typeName = node->typeName();
1663 info.isDefaultTransform = node->style().transform.isDefault();
1664
1665 QTransform xf = !info.isDefaultTransform ? node->style().transform->qtransform() : QTransform();
1666 info.transform.setDefaultValue(QVariant::fromValue(xf));
1667 info.isDefaultOpacity = node->style().opacity.isDefault();
1668 info.opacity.setDefaultValue(!info.isDefaultOpacity ? node->style().opacity->opacity() : 1.0);
1669 info.isVisible = node->isVisible();
1670 info.isDisplayed = node->displayMode() != QSvgNode::DisplayMode::NoneMode;
1671
1672 if (node->hasFilter()
1673 || node->hasMask()
1674 || node->type() == QSvgNode::Type::Mask
1675 || node->type() == QSvgNode::Type::Pattern) {
1676 QImage dummy(1, 1, QImage::Format_RGB32);
1677 QPainter p(&dummy);
1678 QSvgExtraStates states;
1679 p.setPen(QPen(Qt::NoPen));
1680 info.bounds = node->internalBounds(&p, states);
1681 }
1682
1683 if (node->hasMask())
1684 info.maskId = findOrCreateId(node->maskId());
1685
1686 if (node->hasFilter())
1687 info.filterId = findOrCreateId(node->filterId());
1688}
1689
1690QList<QSvgVisitorImpl::AnimationPair> QSvgVisitorImpl::collectAnimations(const QSvgNode *node,
1691 const QString &propertyName)
1692{
1693 QList<AnimationPair> ret;
1694 const QList<QSvgAbstractAnimation *> animations = node->document()->animator()->animationsForNode(node);
1695 for (const QSvgAbstractAnimation *animation : animations) {
1696 const QList<QSvgAbstractAnimatedProperty *> properties = animation->properties();
1697 for (const QSvgAbstractAnimatedProperty *property : properties) {
1698 if (property->propertyName() == propertyName)
1699 ret.append(std::make_pair(animation, property));
1700 }
1701 }
1702
1703 return ret;
1704}
1705
1706void QSvgVisitorImpl::applyAnimationsToProperty(const QList<AnimationPair> &animations,
1707 QQuickAnimatedProperty *outProperty,
1708 std::function<QVariant(const QSvgAbstractAnimatedProperty *, int index, int animationIndex)> calculateValue)
1709{
1710 qCDebug(lcVectorImageAnimations) << "Applying animations to property with default value"
1711 << outProperty->defaultValue();
1712 for (auto it = animations.constBegin(); it != animations.constEnd(); ++it) {
1713 qCDebug(lcVectorImageAnimations) << " -> Add animation";
1714 const QSvgAbstractAnimation *animation = it->first;
1715 const QSvgAbstractAnimatedProperty *property = it->second;
1716
1717 const int start = animation->start();
1718 const int repeatCount = animation->iterationCount();
1719 const int duration = animation->duration();
1720
1721 bool freeze = false;
1722 bool replace = true;
1723 if (animation->animationType() == QSvgAbstractAnimation::SMIL) {
1724 const QSvgAnimateNode *animateNode = static_cast<const QSvgAnimateNode *>(animation);
1725 freeze = animateNode->fill() == QSvgAnimateNode::Freeze;
1726 replace = animateNode->additiveType() == QSvgAnimateNode::Replace;
1727 }
1728
1729 qCDebug(lcVectorImageAnimations) << " -> Start:" << start
1730 << ", repeatCount:" << repeatCount
1731 << ", freeze:" << freeze
1732 << ", replace:" << replace;
1733
1734 QBezier easing = easingForAnimation(animation);
1735 QList<qreal> propertyKeyFrames = property->keyFrames();
1736 QList<QQuickAnimatedProperty::PropertyAnimation> outAnimations;
1737
1738 // For transform animations, we register the type of the transform in the animation
1739 // (this assumes that each animation is only for a single part of the transform)
1740 if (property->type() == QSvgAbstractAnimatedProperty::Transform) {
1741 const auto *transformProperty = static_cast<const QSvgAnimatedPropertyTransform *>(property);
1742 const auto &components = transformProperty->components();
1743 Q_ASSERT(q20::cmp_greater_equal(components.size(),transformProperty->transformCount()));
1744 for (uint i = 0; i < transformProperty->transformCount(); ++i) {
1745 QQuickAnimatedProperty::PropertyAnimation outAnimation;
1746 outAnimation.repeatCount = repeatCount;
1747 outAnimation.startOffset = start;
1748 if (freeze)
1749 outAnimation.flags |= QQuickAnimatedProperty::PropertyAnimation::FreezeAtEnd;
1750 if (replace)
1751 outAnimation.flags |= QQuickAnimatedProperty::PropertyAnimation::ReplacePreviousAnimations;
1752 switch (components.at(i).type) {
1753 case QSvgAnimatedPropertyTransform::TransformComponent::Translate:
1754 outAnimation.subtype = QTransform::TxTranslate;
1755 break;
1756 case QSvgAnimatedPropertyTransform::TransformComponent::Scale:
1757 outAnimation.subtype = QTransform::TxScale;
1758 break;
1759 case QSvgAnimatedPropertyTransform::TransformComponent::Rotate:
1760 outAnimation.subtype = QTransform::TxRotate;
1761 break;
1762 case QSvgAnimatedPropertyTransform::TransformComponent::Skew:
1763 outAnimation.subtype = QTransform::TxShear;
1764 break;
1765 default:
1766 qCWarning(lcQuickVectorImage()) << "Unhandled transform type:" << components.at(i).type;
1767 break;
1768 }
1769
1770 qDebug(lcVectorImageAnimations) << " -> Property type:"
1771 << property->type()
1772 << " name:"
1773 << property->propertyName()
1774 << " animation subtype:"
1775 << outAnimation.subtype;
1776
1777 outAnimations.append(outAnimation);
1778 }
1779 } else {
1780 QQuickAnimatedProperty::PropertyAnimation outAnimation;
1781 outAnimation.repeatCount = repeatCount;
1782 outAnimation.startOffset = start;
1783 if (freeze)
1784 outAnimation.flags |= QQuickAnimatedProperty::PropertyAnimation::FreezeAtEnd;
1785
1786 qDebug(lcVectorImageAnimations) << " -> Property type:"
1787 << property->type()
1788 << " name:"
1789 << property->propertyName();
1790
1791 outAnimations.append(outAnimation);
1792 }
1793
1794 outProperty->beginAnimationGroup();
1795 for (int i = 0; i < outAnimations.size(); ++i) {
1796 QQuickAnimatedProperty::PropertyAnimation outAnimation = outAnimations.at(i);
1797
1798 for (int j = 0; j < propertyKeyFrames.size(); ++j) {
1799 const int time = qRound(propertyKeyFrames.at(j) * duration);
1800
1801 const QVariant value = calculateValue(property, j, i);
1802 outAnimation.frames[time] = value;
1803 outAnimation.easingPerFrame[time] = easing;
1804 qCDebug(lcVectorImageAnimations) << " -> Frame " << time << " is " << value;
1805 }
1806
1807 outProperty->addAnimation(outAnimation);
1808 }
1809 }
1810}
1811
1812QBezier QSvgVisitorImpl::easingForAnimation(const QSvgAbstractAnimation *animation)
1813{
1814 constexpr QPointF startControlPoint(0, 0);
1815 constexpr QPointF endControlPoint(1, 1);
1816 constexpr QPointF easeC1(0.25, 0.1);
1817 constexpr QPointF easeC2(0.25, 1);
1818
1819 QBezier easing = QBezier::fromPoints(startControlPoint, startControlPoint, endControlPoint, endControlPoint);
1820
1821#if QT_CONFIG(cssparser)
1822 if (animation->animationType() == QSvgAbstractAnimation::CSS) {
1823 QSvgEasingInterface *easingInterface = animation->easing();
1824 QSvgCssEasing *cssEasing = static_cast<QSvgCssEasing *>(easingInterface);
1825 switch (cssEasing->easingFunction()) {
1826 case QSvgCssValues::EasingFunction::Ease:
1827 case QSvgCssValues::EasingFunction::EaseIn:
1828 case QSvgCssValues::EasingFunction::EaseOut:
1829 case QSvgCssValues::EasingFunction::EaseInOut:
1830 case QSvgCssValues::EasingFunction::CubicBezier:
1831 case QSvgCssValues::EasingFunction::Linear:
1832 {
1833 const QSvgCssCubicBezierEasing *cssCubicEasing = static_cast<const QSvgCssCubicBezierEasing *>(cssEasing);
1834 QPointF c1 = cssCubicEasing->c1();
1835 QPointF c2 = cssCubicEasing->c2();
1836 easing = QBezier::fromPoints(startControlPoint, c1, c2, endControlPoint);
1837 break;
1838 }
1839 case QSvgCssValues::EasingFunction::Steps:
1840 {
1841 qCDebug(lcVectorImageAnimations) << "Step easing is not supported reverting to default.";
1842 easing = QBezier::fromPoints(startControlPoint, easeC1, easeC2, endControlPoint);
1843 break;
1844 }
1845 }
1846 }
1847#endif
1848
1849 return easing;
1850}
1851
1852void QSvgVisitorImpl::fillColorAnimationInfo(const QSvgNode *node, PathNodeInfo &info)
1853{
1854 // Collect all animations affecting fill
1855 {
1856 QList<AnimationPair> animations = collectAnimations(node, QStringLiteral("fill"));
1857 if (!animations.isEmpty())
1858 applyAnimationsToProperty(animations, &info.fillColor, calculateInterpolatedValue);
1859 }
1860
1861 {
1862 QList<AnimationPair> animations = collectAnimations(node, QStringLiteral("fill-opacity"));
1863 if (!animations.isEmpty())
1864 applyAnimationsToProperty(animations, &info.fillOpacity, calculateInterpolatedValue);
1865 }
1866
1867 {
1868 QList<AnimationPair> animations = collectAnimations(node, QStringLiteral("stroke"));
1869 if (!animations.isEmpty())
1870 applyAnimationsToProperty(animations, &info.strokeStyle.color, calculateInterpolatedValue);
1871 }
1872
1873 {
1874 QList<AnimationPair> animations = collectAnimations(node, QStringLiteral("stroke-opacity"));
1875 if (!animations.isEmpty())
1876 applyAnimationsToProperty(animations, &info.strokeStyle.opacity, calculateInterpolatedValue);
1877 }
1878}
1879
1880void QSvgVisitorImpl::fillTransformAnimationInfo(const QSvgNode *node, NodeInfo &info)
1881{
1882 qCDebug(lcVectorImageAnimations) << "Applying transform animations to property with default value"
1883 << info.transform.defaultValue();
1884
1885 auto calculateValue = [](const QSvgAbstractAnimatedProperty *property, int index, int animationIndex) {
1886 if (property->type() != QSvgAbstractAnimatedProperty::Transform)
1887 return QVariant{};
1888
1889 const auto *transformProperty = static_cast<const QSvgAnimatedPropertyTransform *>(property);
1890 const auto &components = transformProperty->components();
1891
1892 const int componentIndex = index * transformProperty->transformCount() + animationIndex;
1893
1894 QVariantList parameters;
1895
1896 const QSvgAnimatedPropertyTransform::TransformComponent &component = components.at(componentIndex);
1897 switch (component.type) {
1898 case QSvgAnimatedPropertyTransform::TransformComponent::Translate:
1899 parameters.append(QVariant::fromValue(QPointF(component.values.value(0),
1900 component.values.value(1))));
1901 break;
1902 case QSvgAnimatedPropertyTransform::TransformComponent::Rotate:
1903 parameters.append(QVariant::fromValue(QPointF(component.values.value(1),
1904 component.values.value(2))));
1905 parameters.append(QVariant::fromValue(component.values.value(0)));
1906 break;
1907 case QSvgAnimatedPropertyTransform::TransformComponent::Scale:
1908 parameters.append(QVariant::fromValue(QPointF(component.values.value(0),
1909 component.values.value(1))));
1910 break;
1911 case QSvgAnimatedPropertyTransform::TransformComponent::Skew:
1912 parameters.append(QVariant::fromValue(QPointF(component.values.value(0),
1913 component.values.value(1))));
1914 break;
1915 default:
1916 qCWarning(lcVectorImageAnimations) << "Unhandled transform type:" << component.type;
1917 };
1918
1919 return QVariant::fromValue(parameters);
1920 };
1921
1922
1923 {
1924 QList<AnimationPair> animations = collectAnimations(node, QStringLiteral("transform"));
1925 if (!animations.isEmpty())
1926 applyAnimationsToProperty(animations, &info.transform, calculateValue);
1927 }
1928}
1929
1930void QSvgVisitorImpl::fillMotionPathAnimationInfo(const QSvgNode *node, NodeInfo &info)
1931{
1932 QList<AnimationPair> animations = collectAnimations(node, QStringLiteral("offset-distance"));
1933 if (animations.isEmpty())
1934 return;
1935
1936 if (animations.size() > 1) {
1937 qCWarning(lcQuickVectorImage)
1938 << "Not supported: More than one offset path animation on same node";
1939 }
1940
1941 if (node->style().offset == nullptr) {
1942 qCWarning(lcQuickVectorImage) << "Motion path animation: No offset path";
1943 return;
1944 }
1945
1946 const AnimationPair &animationPair = animations.first();
1947
1948 const QSvgAbstractAnimation *animation = animationPair.first;
1949 const QSvgAbstractAnimatedProperty *property = animationPair.second;
1950
1951 const int start = animation->start();
1952 const int repeatCount = animation->iterationCount();
1953 const int duration = animation->duration();
1954
1955 qCDebug(lcVectorImageAnimations) << "Motion path animation:"
1956 << "start == " << start
1957 << ", repeatCount == " << repeatCount
1958 << "; duration == " << duration;
1959
1960
1961 QQuickAnimatedProperty::PropertyAnimation outAnimation;
1962 outAnimation.repeatCount = repeatCount;
1963 outAnimation.startOffset = start;
1964
1965 QPainterPath originalPath = node->style().offset->path();
1966
1967 qreal baseRotation;
1968 bool adaptAngle;
1969 switch (node->style().offset->rotateType()) {
1970 case QtSvg::OffsetRotateType::Auto:
1971 adaptAngle = true;
1972 baseRotation = 0.0;
1973 break;
1974 case QtSvg::OffsetRotateType::Angle:
1975 adaptAngle = false;
1976 baseRotation = node->style().offset->rotateAngle();
1977 break;
1978 case QtSvg::OffsetRotateType::AutoAngle:
1979 adaptAngle = true;
1980 baseRotation = node->style().offset->rotateAngle();
1981 break;
1982 case QtSvg::OffsetRotateType::Reverse:
1983 adaptAngle = true;
1984 baseRotation = 180.0;
1985 break;
1986 case QtSvg::OffsetRotateType::ReverseAngle:
1987 adaptAngle = true;
1988 baseRotation = node->style().offset->rotateAngle() + 180.0f;
1989 break;
1990 default:
1991 Q_UNREACHABLE();
1992 }
1993
1994 // Default value holds additional parameters
1995 info.motionPath.setDefaultValue(QVariant::fromValue(QVariantPair(adaptAngle, baseRotation)));
1996
1997 const QList<qreal> propertyKeyFrames = property->keyFrames();
1998
1999 qreal previousT = 0.0;
2000 for (int j = 0; j < propertyKeyFrames.size(); ++j) {
2001 const int time = qRound(propertyKeyFrames.at(j) * duration);
2002
2003 qreal t = calculateInterpolatedValue(property, j, 0).toReal();
2004
2005 if (time > 0) {
2006 QPainterPath path = originalPath.trimmed(previousT, t);
2007
2008 outAnimation.frames[time] = QVariant::fromValue(path);
2009 qCDebug(lcVectorImageAnimations) << " -> Frame " << time << " is " << path;
2010 }
2011
2012 previousT = t;
2013 }
2014
2015 info.motionPath.addAnimation(outAnimation);
2016}
2017
2018void QSvgVisitorImpl::fillPathAnimationInfo(const QSvgNode *node, PathNodeInfo &info)
2019{
2020 fillColorAnimationInfo(node, info);
2021 fillAnimationInfo(node, info);
2022}
2023
2024void QSvgVisitorImpl::fillAnimationInfo(const QSvgNode *node, NodeInfo &info)
2025{
2026 {
2027 QList<AnimationPair> animations = collectAnimations(node, QStringLiteral("opacity"));
2028 if (!animations.isEmpty())
2029 applyAnimationsToProperty(animations, &info.opacity, calculateInterpolatedValue);
2030 }
2031
2032 fillTransformAnimationInfo(node, info);
2033 fillMotionPathAnimationInfo(node, info);
2034}
2035
2036void QSvgVisitorImpl::handleBaseNodeSetup(const QSvgNode *node)
2037{
2038 qCDebug(lcQuickVectorImage) << "Before SETUP" << node << "fill" << m_styleResolver->currentFillColor()
2039 << "stroke" << m_styleResolver->currentStrokeColor() << m_styleResolver->currentStrokeWidth()
2040 << node->nodeId() << " type: " << node->typeName() << " " << node->type();
2041
2042 node->applyStyle(&m_styleResolver->painter(), m_styleResolver->states());
2043
2044 qCDebug(lcQuickVectorImage) << "After SETUP" << node << "fill" << m_styleResolver->currentFillColor()
2045 << "stroke" << m_styleResolver->currentStrokeColor()
2046 << m_styleResolver->currentStrokeWidth() << node->nodeId();
2047}
2048
2049void QSvgVisitorImpl::handleBaseNode(const QSvgNode *node)
2050{
2051 NodeInfo info;
2052 fillCommonNodeInfo(node, info);
2053
2054 m_generator->generateNodeBase(info);
2055}
2056
2057void QSvgVisitorImpl::handleBaseNodeEnd(const QSvgNode *node)
2058{
2059 node->revertStyle(&m_styleResolver->painter(), m_styleResolver->states());
2060
2061 qCDebug(lcQuickVectorImage) << "After END" << node << "fill" << m_styleResolver->currentFillColor()
2062 << "stroke" << m_styleResolver->currentStrokeColor() << m_styleResolver->currentStrokeWidth()
2063 << node->nodeId();
2064}
2065
2066void QSvgVisitorImpl::handlePathNode(const QSvgNode *node, const QPainterPath &path)
2067{
2068 handleBaseNodeSetup(node);
2069
2070 PathNodeInfo info;
2071 fillCommonNodeInfo(node, info);
2072
2073 if (node->hasMarkerStart())
2074 info.markerStartId = findOrCreateId(node->markerStartId());
2075
2076 if (node->hasMarkerMid())
2077 info.markerMidId = findOrCreateId(node->markerMidId());
2078
2079 if (node->hasMarkerEnd())
2080 info.markerEndId = findOrCreateId(node->markerEndId());
2081
2082 const QGradient *strokeGradient = m_styleResolver->currentStrokeGradient();
2083 auto strokeStyle = node->style().stroke;
2084 bool hasStrokePattern = strokeStyle
2085 && strokeStyle->style()
2086 && strokeStyle->style()->type() == QSvgStyleProperty::PATTERN;
2087
2088 info.path.setDefaultValue(QVariant::fromValue(path));
2089 info.fillColor.setDefaultValue(m_styleResolver->currentFillColor());
2090 if (strokeGradient == nullptr && !hasStrokePattern) {
2091 info.strokeStyle = StrokeStyle::fromPen(m_styleResolver->currentStroke());
2092 info.strokeStyle.color.setDefaultValue(m_styleResolver->currentStrokeColor());
2093 }
2094 if (m_styleResolver->currentFillGradient() != nullptr)
2095 info.grad = m_styleResolver->applyOpacityToGradient(*m_styleResolver->currentFillGradient(), m_styleResolver->currentFillOpacity());
2096 info.fillTransform = m_styleResolver->currentFillTransform();
2097
2098 auto fillStyle = node->style().fill;
2099 if (fillStyle) {
2100 info.fillRule = fillStyle->fillRule();
2101
2102 if (fillStyle->style() && fillStyle->style()->type() == QSvgStyleProperty::PATTERN) {
2103 QSvgPatternStyle *patternStyle = static_cast<QSvgPatternStyle *>(fillStyle->style());
2104 info.patternId = findOrCreateId(patternStyle->patternNode()->nodeId());
2105
2106 // The fill transform in the style resolver is a calculated transform which contains
2107 // the inverse of the QPainter's world transform at the given time to negate any other
2108 // transform set. We avoid this by generating the pattern definition in isolation and
2109 // ignore its transform, so we just use the raw pattern transform from the input here.
2110 info.fillTransform = patternStyle->patternNode()->transform();
2111 }
2112 }
2113
2114 fillPathAnimationInfo(node, info);
2115
2116 m_generator->generatePath(info);
2117
2118 if (strokeGradient != nullptr || hasStrokePattern) {
2119 PathNodeInfo strokeInfo;
2120 fillCommonNodeInfo(node, strokeInfo, QStringLiteral("_stroke"));
2121
2122 if (strokeGradient != nullptr) {
2123 strokeInfo.grad = *strokeGradient;
2124 } else {
2125 QSvgPatternStyle *patternStyle = static_cast<QSvgPatternStyle *>(strokeStyle->style());
2126 strokeInfo.patternId = findOrCreateId(patternStyle->patternNode()->nodeId());
2127 strokeInfo.fillTransform = patternStyle->patternNode()->transform();
2128 }
2129
2130 QPainterPathStroker stroker(m_styleResolver->currentStroke());
2131 strokeInfo.path.setDefaultValue(QVariant::fromValue(stroker.createStroke(path)));
2132 m_generator->generatePath(strokeInfo);
2133 }
2134
2135 handleBaseNodeEnd(node);
2136}
2137
2138void QSvgVisitorImpl::fillMarkerInfo(const QSvgMarker *node, MarkerNodeInfo &info)
2139{
2140 QTransform oldTransform = info.transform.defaultValue().value<QTransform>();
2141
2142 info.markerSize = node->rect().size();
2143 info.anchorPoint = node->refP();
2144 info.clipBox = oldTransform.mapRect(node->clipRect());
2145 info.viewBox = node->viewBox();
2146 switch (node->orientation()) {
2147 case QSvgMarker::Orientation::Auto:
2149 break;
2150 case QSvgMarker::Orientation::AutoStartReverse:
2152 break;
2153 case QSvgMarker::Orientation::Value:
2155 break;
2156 }
2157
2158 switch (node->markerUnits()) {
2159 case QSvgMarker::MarkerUnits::UserSpaceOnUse:
2161 break;
2162 case QSvgMarker::MarkerUnits::StrokeWidth:
2164 break;
2165 }
2166
2167 info.angle = node->orientationAngle();
2168
2169 QTransform xform = node->aspectRatioTransform();
2170 if (!xform.isIdentity()) {
2171 info.isDefaultTransform = false;
2172 xform = xform * oldTransform;
2173 info.transform.setDefaultValue(QVariant::fromValue(xform));
2174 }
2175
2176 info.preserveAspectRatio = MarkerNodeInfo::PreserveAspectRatio(node->preserveAspectRatios().toInt());
2177}
2178
2179bool QSvgVisitorImpl::visitMarkerNodeStart(const QSvgMarker *node)
2180{
2181 if (!m_pregeneratingReferencedNodes)
2182 return false;
2183
2184 handleBaseNodeSetup(node);
2185
2186 MarkerNodeInfo info;
2187
2188 fillCommonNodeInfo(node, info);
2189 fillAnimationInfo(node, info);
2190 fillMarkerInfo(node, info);
2191 info.stage = StructureNodeStage::Start;
2192
2193 return m_generator->generateMarkerNode(info);
2194}
2195
2196void QSvgVisitorImpl::visitMarkerNodeEnd(const QSvgMarker *node)
2197{
2198 handleBaseNodeEnd(node);
2199
2200 MarkerNodeInfo info;
2201 fillCommonNodeInfo(node, info);
2202 fillMarkerInfo(node, info);
2203 info.stage = StructureNodeStage::End;
2204
2205 m_generator->generateMarkerNode(info);
2206}
2207
2208QT_END_NAMESPACE
const QGradient * currentFillGradient() const
const QGradient * currentStrokeGradient() const
QColor currentFillColor() const
QSvgExtraStates & states()
static QGradient applyOpacityToGradient(const QGradient &gradient, float opacity)
QTransform currentFillTransform() const
QPen currentStroke() const
qreal currentFillOpacity() const
float currentStrokeWidth() const
QSvgExtraStates m_svgState
QColor currentStrokeColor() const
bool visitFilterNodeStart(const QSvgFilterContainer *node) override
void visitPolygonNode(const QSvgPolygon *node) override
bool visitSwitchNodeStart(const QSvgSwitch *node) override
void visitMaskNodeEnd(const QSvgMask *node) override
void visitEllipseNode(const QSvgEllipse *node) override
void visitPathNode(const QSvgPath *node) override
void visitMarkerNodeEnd(const QSvgMarker *node) override
bool visitDefsNodeStart(const QSvgDefs *node) override
void visitDefsNodeEnd(const QSvgDefs *node) override
bool visitMaskNodeStart(const QSvgMask *node) override
void visitDocumentNodeEnd(const QSvgDocument *node) override
~QSvgVisitorImpl() override
void visitStructureNodeEnd(const QSvgStructureNode *node) override
void visitRectNode(const QSvgRect *node) override
void visitFeFilterPrimitiveNodeEnd(const QSvgFeFilterPrimitive *node) override
void visitLineNode(const QSvgLine *node) override
void visitNode(const QSvgNode *node) override
bool visitMarkerNodeStart(const QSvgMarker *node) override
void visitPolylineNode(const QSvgPolyline *node) override
QSvgVisitorImpl(const QString svgFileName, QQuickGenerator *generator, bool assumeTrustedSource)
void visitUseNode(const QSvgUse *node) override
bool visitSymbolNodeStart(const QSvgSymbol *node) override
void visitPatternNodeEnd(const QSvgPattern *) override
bool visitDocumentNodeStart(const QSvgDocument *node) override
void visitImageNode(const QSvgImage *node) override
void visitFilterNodeEnd(const QSvgFilterContainer *node) override
bool visitPatternNodeStart(const QSvgPattern *) override
void visitTextNode(const QSvgText *node) override
void visitSwitchNodeEnd(const QSvgSwitch *node) override
void visitSymbolNodeEnd(const QSvgSymbol *node) override
bool visitFeFilterPrimitiveNodeStart(const QSvgFeFilterPrimitive *node) override
bool visitStructureNodeStart(const QSvgStructureNode *node) override
Combined button and popup list for selecting options.
static QString scrub(const QString &raw)
static bool isStructureNode(const QSvgNode *node)
static void recurseSvgNodes(const QSvgNode *root, const std::function< void(const QSvgNode *)> &fnc)
static QVariant calculateInterpolatedValue(const QSvgAbstractAnimatedProperty *property, int index, int)