aboutsummaryrefslogtreecommitdiffstats
path: root/src/quickvectorimage/generator/utils_p.h
blob: 0c25f3d7d3a04d9df81620630d00b32e76109b54 (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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
// 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

#ifndef UTILS_P_H
#define UTILS_P_H

//
//  W A R N I N G
//  -------------
//
// This file is not part of the Qt API.  It exists purely as an
// implementation detail.  This header file may change from version to
// version without notice, or even be removed.
//
// We mean it.
//

#include <private/qquicktranslate_p.h>
#include <private/qquickitem_p.h>
#include <private/qsvgnode_p.h>

#include <private/qquadpath_p.h>
#include <private/qsvgvisitor_p.h>

QT_BEGIN_NAMESPACE

namespace QQuickVectorImageGenerator::Utils
{

class ViewBoxItem : public QQuickItem
{
public:
    ViewBoxItem(const QRectF viewBox, QQuickItem *parent = nullptr) : QQuickItem(parent), m_viewBox(viewBox) { setXForm(); }

protected:
    void geometryChange(const QRectF &/*newGeometry*/, const QRectF &/*oldGeometry*/) override
    {
        setXForm();
    }

private:
    void setXForm()
    {
        auto xformProp = transform();
        xformProp.clear(&xformProp);
        bool translate = !qFuzzyIsNull(m_viewBox.x()) || !qFuzzyIsNull(m_viewBox.y());
        if (translate) {
            auto *tr = new QQuickTranslate(this);
            tr->setX(-m_viewBox.x());
            tr->setY(-m_viewBox.y());
            xformProp.append(&xformProp, tr);
        }
        if (!m_viewBox.isEmpty() && width() && height()) {
            auto *scale = new QQuickScale(this);
            qreal sx = width() / m_viewBox.width();
            qreal sy = height() / m_viewBox.height();

            scale->setXScale(sx);
            scale->setYScale(sy);
            xformProp.append(&xformProp, scale);
        }
    }
    QRectF m_viewBox;
};

inline QPainterPath polygonToPath(const QPolygonF &poly, bool closed)
{
    QPainterPath path;
    if (poly.isEmpty())
        return path;
    bool first = true;
    for (const auto &p : poly) {
        if (first)
            path.moveTo(p);
        else
            path.lineTo(p);
        first = false;
    }
    if (closed)
        path.closeSubpath();
    return path;
}

inline QString pathHintString(const QQuadPath &qp)
{
    QString res;
    QTextStream str(&res);
    auto flags = qp.pathHints();
    if (!flags)
        return res;
    str << "pathHints:";
    bool first = true;

#define CHECK_PATH_HINT(flagName)              \
    if (flags.testFlag(QQuadPath::flagName)) { \
            if (!first)                            \
            str << " |";                       \
            first = false;                         \
            str << " ShapePath." #flagName;        \
    }

    CHECK_PATH_HINT(PathLinear)
    CHECK_PATH_HINT(PathQuadratic)
    CHECK_PATH_HINT(PathConvex)
    CHECK_PATH_HINT(PathFillOnRight)
    CHECK_PATH_HINT(PathSolid)
    CHECK_PATH_HINT(PathNonIntersecting)
    CHECK_PATH_HINT(PathNonOverlappingControlPointTriangles)

    return res;
}

// Find the square that gives the same gradient in QGradient::LogicalMode as
// objModeRect does in QGradient::ObjectMode

// When the object's bounding box is not square, the stripes that are conceptually
// perpendicular to the gradient vector within object bounding box space shall render
// non-perpendicular relative to the gradient vector in user space due to application
// of the non-uniform scaling transformation from bounding box space to user space.
inline QRectF mapToQtLogicalMode(const QRectF &objModeRect, const QRectF &boundingRect)
{

    QRect pixelRect(objModeRect.x() * boundingRect.width() + boundingRect.left(),
                    objModeRect.y() * boundingRect.height() + boundingRect.top(),
                    objModeRect.width() * boundingRect.width(),
                    objModeRect.height() * boundingRect.height());

    if (pixelRect.isEmpty()) // pure horizontal/vertical gradient
        return pixelRect;

    double w = boundingRect.width();
    double h = boundingRect.height();
    double objModeSlope = objModeRect.height() / objModeRect.width();
    double a = objModeSlope * w / h;

    // do calculation with origin == pixelRect.topLeft
    double x2 = pixelRect.width();
    double y2 = pixelRect.height();
    double x = (x2 + a * y2) / (1 + a * a);
    double y = y2 - (x - x2)/a;

    return QRectF(pixelRect.topLeft(), QSizeF(x,y));
}

inline QString toSvgString(const QPainterPath &path)
{
    QString svgPathString;
    QTextStream strm(&svgPathString);

    for (int i = 0; i < path.elementCount(); ++i) {
        QPainterPath::Element element = path.elementAt(i);
        if (element.isMoveTo()) {
            strm << "M " << element.x << " " << element.y << " ";
        } else if (element.isLineTo()) {
            strm << "L " << element.x << " " << element.y << " ";
        } else if (element.isCurveTo()) {
            QPointF c1(element.x, element.y);
            ++i;
            element = path.elementAt(i);

            QPointF c2(element.x, element.y);
            ++i;
            element = path.elementAt(i);
            QPointF ep(element.x, element.y);

            strm <<  "C "
                 <<  c1.x() << " "
                 <<  c1.y() << " "
                 <<  c2.x() << " "
                 <<  c2.y() << " "
                 <<  ep.x() << " "
                 <<  ep.y() << " ";
        }
    }

    return svgPathString;
}

inline QString toSvgString(const QQuadPath &path)
{
    QString svgPathString;
    QTextStream strm(&svgPathString);
    path.iterateElements([&](const QQuadPath::Element &e, int) {
        if (e.isSubpathStart())
            strm << "M " << e.startPoint().x() << " " << e.startPoint().y() << " ";
        if (e.isLine())
            strm << "L " << e.endPoint().x() << " " << e.endPoint().y() << " ";
        else
            strm << "Q " << e.controlPoint().x() << " " << e.controlPoint().y() << " "
                 << e.endPoint().x() << " " << e.endPoint().y() << " ";
    });

    return svgPathString;
}

inline QString strokeCapStyleString(Qt::PenCapStyle strokeCapStyle)
{
    QString capStyle;
    switch (strokeCapStyle) {
    case Qt::FlatCap:
        capStyle = QStringLiteral("ShapePath.FlatCap");
        break;
    case Qt::SquareCap:
        capStyle = QStringLiteral("ShapePath.SquareCap");
        break;
    case Qt::RoundCap:
        capStyle = QStringLiteral("ShapePath.RoundCap");
        break;
    default:
        Q_UNREACHABLE();
        break;
    }

    return capStyle;
}

inline QString strokeJoinStyleString(Qt::PenJoinStyle strokeJoinStyle)
{
    QString joinStyle;
    switch (strokeJoinStyle) {
    case Qt::MiterJoin:
        joinStyle = QStringLiteral("ShapePath.MiterJoin");
        break;
    case Qt::BevelJoin:
        joinStyle = QStringLiteral("ShapePath.BevelJoin");
        break;
    case Qt::RoundJoin:
        joinStyle = QStringLiteral("ShapePath.RoundJoin");
        break;
    default:
        //TODO: Add support for SvgMiter case
        Q_UNREACHABLE();
        break;
    }

    return joinStyle;
}

template<typename T>
inline QString listString(QList<T> list)
{
    if (list.isEmpty())
        return QStringLiteral("[]");

    QString l;
    QTextStream stream(&l);
    stream << "[";

    if (list.length() > 1) {
        for (int i = 0; i < list.length() - 1; i++) {
            T v = list[i];
            stream << v << ", ";
        }
    }

    stream << list.last() << "]";
    return l;
}

}

QT_END_NAMESPACE

#endif // UTILS_P_H