| 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 | |
| 4 | #include <QVarLengthArray> |
| 5 | |
| 6 | #include "qquickgenerator_p.h" |
| 7 | #include "qsvgvisitorimpl_p.h" |
| 8 | #include "qquicknodeinfo_p.h" |
| 9 | |
| 10 | #include <private/qsgcurveprocessor_p.h> |
| 11 | #include <private/qquickshape_p.h> |
| 12 | #include <private/qquadpath_p.h> |
| 13 | #include <private/qquickitem_p.h> |
| 14 | #include <private/qquickimagebase_p_p.h> |
| 15 | |
| 16 | #include <QtCore/qloggingcategory.h> |
| 17 | |
| 18 | QT_BEGIN_NAMESPACE |
| 19 | |
| 20 | Q_LOGGING_CATEGORY(lcQuickVectorImage, "qt.quick.vectorimage" , QtWarningMsg) |
| 21 | |
| 22 | QQuickGenerator::QQuickGenerator(const QString fileName, QQuickVectorImageGenerator::GeneratorFlags flags) |
| 23 | : m_flags(flags) |
| 24 | , m_fileName(fileName) |
| 25 | { |
| 26 | } |
| 27 | |
| 28 | QQuickGenerator::~QQuickGenerator() |
| 29 | { |
| 30 | } |
| 31 | |
| 32 | void QQuickGenerator::setGeneratorFlags(QQuickVectorImageGenerator::GeneratorFlags flags) |
| 33 | { |
| 34 | m_flags = flags; |
| 35 | } |
| 36 | |
| 37 | QQuickVectorImageGenerator::GeneratorFlags QQuickGenerator::generatorFlags() |
| 38 | { |
| 39 | return m_flags; |
| 40 | } |
| 41 | |
| 42 | bool QQuickGenerator::generate() |
| 43 | { |
| 44 | QSvgVisitorImpl loader(m_fileName, this, m_flags.testFlag(flag: QQuickVectorImageGenerator::AssumeTrustedSource)); |
| 45 | return loader.traverse(); |
| 46 | } |
| 47 | |
| 48 | void QQuickGenerator::optimizePaths(const PathNodeInfo &info, const QRectF &overrideBoundingRect) |
| 49 | { |
| 50 | QPainterPath pathCopy = info.painterPath; |
| 51 | pathCopy.setFillRule(info.fillRule); |
| 52 | |
| 53 | const QRectF &boundingRect = overrideBoundingRect.isNull() ? pathCopy.boundingRect() : overrideBoundingRect; |
| 54 | if (m_flags.testFlag(flag: QQuickVectorImageGenerator::GeneratorFlag::OptimizePaths)) { |
| 55 | QQuadPath strokePath = QQuadPath::fromPainterPath(path: pathCopy); |
| 56 | bool fillPathNeededClose; |
| 57 | QQuadPath fillPath = strokePath.subPathsClosed(didClose: &fillPathNeededClose); |
| 58 | const bool intersectionsFound = QSGCurveProcessor::solveIntersections(path&: fillPath, removeNestedPaths: false); |
| 59 | fillPath.addCurvatureData(); |
| 60 | QSGCurveProcessor::solveOverlaps(path&: fillPath); |
| 61 | const bool compatibleStrokeAndFill = !fillPathNeededClose && !intersectionsFound; |
| 62 | |
| 63 | if (compatibleStrokeAndFill || m_flags.testFlag(flag: QQuickVectorImageGenerator::GeneratorFlag::OutlineStrokeMode)) { |
| 64 | outputShapePath(info, path: nullptr, quadPath: &fillPath, pathSelector: QQuickVectorImageGenerator::FillAndStroke, boundingRect); |
| 65 | } else { |
| 66 | outputShapePath(info, path: nullptr, quadPath: &fillPath, pathSelector: QQuickVectorImageGenerator::FillPath, boundingRect); |
| 67 | outputShapePath(info, path: nullptr, quadPath: &strokePath, pathSelector: QQuickVectorImageGenerator::StrokePath, boundingRect); |
| 68 | } |
| 69 | } else { |
| 70 | outputShapePath(info, path: &pathCopy, quadPath: nullptr, pathSelector: QQuickVectorImageGenerator::FillAndStroke, boundingRect); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | bool QQuickGenerator::isNodeVisible(const NodeInfo &info) |
| 75 | { |
| 76 | if (!info.isVisible || !info.isDisplayed) |
| 77 | return false; |
| 78 | |
| 79 | return true; |
| 80 | } |
| 81 | |
| 82 | QT_END_NAMESPACE |
| 83 | |