aboutsummaryrefslogtreecommitdiffstats
path: root/src/quickvectorimage/generator/qquickgenerator.cpp
blob: 505b10f919ade7e285da1ee3cd46c5240c83fd73 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// Copyright (C) 2024 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#include "qquickgenerator_p.h"
#include "qsvgvisitorimpl_p.h"
#include "qquicknodeinfo_p.h"

#include <private/qsgcurveprocessor_p.h>
#include <private/qquickshape_p.h>
#include <private/qquadpath_p.h>
#include <private/qquickitem_p.h>
#include <private/qquickimagebase_p_p.h>

#include <QtCore/qloggingcategory.h>

QT_BEGIN_NAMESPACE

Q_LOGGING_CATEGORY(lcQuickVectorImage, "qt.quick.vectorimage", QtWarningMsg)

QQuickGenerator::QQuickGenerator(const QString fileName, QQuickVectorImageGenerator::GeneratorFlags flags)
    : m_flags(flags)
    , m_fileName(fileName)
    , m_loader(nullptr)
{
}

QQuickGenerator::~QQuickGenerator()
{
    delete m_loader;
}

void QQuickGenerator::setGeneratorFlags(QQuickVectorImageGenerator::GeneratorFlags flags)
{
    m_flags = flags;
}

QQuickVectorImageGenerator::GeneratorFlags QQuickGenerator::generatorFlags()
{
    return m_flags;
}

void QQuickGenerator::generate()
{
    m_loader = new QSvgVisitorImpl(m_fileName, this);
    m_loader->traverse();
}

void QQuickGenerator::optimizePaths(const PathNodeInfo &info)
{
    QPainterPath pathCopy = info.painterPath;
    pathCopy.setFillRule(info.fillRule);

    if (m_flags.testFlag(QQuickVectorImageGenerator::GeneratorFlag::OptimizePaths)) {
        QQuadPath strokePath = QQuadPath::fromPainterPath(pathCopy);
        bool fillPathNeededClose;
        QQuadPath fillPath = strokePath.subPathsClosed(&fillPathNeededClose);
        const bool intersectionsFound = QSGCurveProcessor::solveIntersections(fillPath, false);
        fillPath.addCurvatureData();
        QSGCurveProcessor::solveOverlaps(fillPath);
        const bool compatibleStrokeAndFill = !fillPathNeededClose && !intersectionsFound;
        if (compatibleStrokeAndFill || m_flags.testFlag(QQuickVectorImageGenerator::GeneratorFlag::OutlineStrokeMode)) {
            outputShapePath(info, nullptr, &fillPath, QQuickVectorImageGenerator::FillAndStroke, pathCopy.boundingRect());
        } else {
            outputShapePath(info, nullptr, &fillPath, QQuickVectorImageGenerator::FillPath, pathCopy.boundingRect());
            outputShapePath(info, nullptr, &strokePath, QQuickVectorImageGenerator::StrokePath, pathCopy.boundingRect());
        }
    } else {
        outputShapePath(info, &pathCopy, nullptr, QQuickVectorImageGenerator::FillAndStroke, pathCopy.boundingRect());
    }
}

bool QQuickGenerator::isNodeVisible(const NodeInfo &info)
{
    if (!info.isVisible || !info.isDisplayed)
        return false;

    return true;
}

QT_END_NAMESPACE