aboutsummaryrefslogtreecommitdiffstats
path: root/src/quick/scenegraph/util/qquadpath_p.h
blob: 98ef5b664ccf63eae98b68a79b2f2d9ff20d1180 (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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
// Copyright (C) 2022 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 QQUADPATH_P_H
#define QQUADPATH_P_H

//
//  W A R N I N G
//  -------------
//
// This file is not part of the Qt API.  It exists for the convenience
// of a number of Qt sources files.  This header file may change from
// version to version without notice, or even be removed.
//
// We mean it.
//

#include <QtCore/qrect.h>
#include <QtCore/qlist.h>
#include <QtCore/qdebug.h>
#include <QtGui/qvector2d.h>
#include <QtGui/qpainterpath.h>
#include <QtQuick/qtquickexports.h>

QT_BEGIN_NAMESPACE

class Q_QUICK_EXPORT QQuadPath
{
public:
    // This is a copy of the flags in QQuickShapePath ### TODO: use a common definition
    enum PathHint : quint8 {
        PathLinear = 0x1,
        PathQuadratic = 0x2,
        PathConvex = 0x4,
        PathFillOnRight = 0x8,
        PathSolid = 0x10,
        PathNonIntersecting = 0x20,
        PathNonOverlappingControlPointTriangles = 0x40
    };
    Q_DECLARE_FLAGS(PathHints, PathHint)

    class Q_QUICK_EXPORT Element
    {
    public:
        Element ()
            : m_isSubpathStart(false), m_isSubpathEnd(false), m_isLine(false)
        {
        }

        Element (QVector2D s, QVector2D c, QVector2D e)
            : sp(s), cp(c), ep(e), m_isSubpathStart(false), m_isSubpathEnd(false), m_isLine(false)
        {
        }

        bool isSubpathStart() const
        {
            return m_isSubpathStart;
        }

        bool isSubpathEnd() const
        {
            return m_isSubpathEnd;
        }

        bool isLine() const
        {
            return m_isLine;
        }

        bool isConvex() const
        {
            return m_curvatureFlags & Convex;
        }

        QVector2D startPoint() const
        {
            return sp;
        }

        QVector2D controlPoint() const
        {
            return cp;
        }

        QVector2D endPoint() const
        {
            return ep;
        }

        QVector2D midPoint() const
        {
            return isLine() ? 0.5f * (sp + ep) : (0.25f * sp) + (0.5f * cp) + (0.25 * ep);
        }

        /* For a curve, returns the control point. For a line, returns an arbitrary point on the
         * inside side of the line (assuming the curvature has been set for the path). The point
         * doesn't need to actually be inside the shape: it just makes for easier calculations
         * later when it is at the same side as the fill. */
        QVector2D referencePoint() const
        {
            if (isLine()) {
                QVector2D normal(sp.y() - ep.y(), ep.x() - sp.x());
                return m_curvatureFlags & Element::FillOnRight ? sp + normal : sp - normal;
            } else {
                return cp;
            }
        }

        Element segmentFromTo(float t0, float t1) const;

        Element reversed() const;

        int childCount() const { return m_numChildren; }

        int indexOfChild(int childNumber) const
        {
            Q_ASSERT(childNumber >= 0 && childNumber < childCount());
            return -(m_firstChildIndex + 1 + childNumber);
        }

        QVector2D pointAtFraction(float t) const;

        QVector2D tangentAtFraction(float t) const
        {
            return isLine() ? (ep - sp) : ((1 - t) * 2 * (cp - sp)) + (t * 2 * (ep - cp));
        }

        QVector2D normalAtFraction(float t) const
        {
            const QVector2D tan = tangentAtFraction(t);
            return QVector2D(-tan.y(), tan.x());
        }

        float extent() const;

        void setAsConvex(bool isConvex)
        {
            if (isConvex)
                m_curvatureFlags = Element::CurvatureFlags(m_curvatureFlags | Element::Convex);
            else
                m_curvatureFlags = Element::CurvatureFlags(m_curvatureFlags & ~Element::Convex);
        }

        void setFillOnRight(bool isFillOnRight)
        {
            if (isFillOnRight)
                m_curvatureFlags = Element::CurvatureFlags(m_curvatureFlags | Element::FillOnRight);
            else
                m_curvatureFlags = Element::CurvatureFlags(m_curvatureFlags & ~Element::FillOnRight);
        }

        bool isFillOnRight() const { return m_curvatureFlags & FillOnRight; }

        bool isControlPointOnLeft() const
        {
            return isPointOnLeft(cp, sp, ep);
        }

        enum CurvatureFlags : quint8 {
            CurvatureUndetermined = 0,
            FillOnRight = 1,
            Convex = 2
        };

        enum FillSide : quint8 {
            FillSideUndetermined = 0,
            FillSideRight = 1,
            FillSideLeft = 2,
            FillSideBoth = 3
        };

    private:
        int intersectionsAtY(float y, float *fractions, bool swapXY = false) const;

        QVector2D sp;
        QVector2D cp;
        QVector2D ep;
        int m_firstChildIndex = 0;
        quint8 m_numChildren = 0;
        CurvatureFlags m_curvatureFlags = CurvatureUndetermined;
        quint8 m_isSubpathStart : 1;
        quint8 m_isSubpathEnd : 1;
        quint8 m_isLine : 1;
        friend class QQuadPath;
        friend Q_QUICK_EXPORT QDebug operator<<(QDebug, const QQuadPath::Element &);
    };

    void moveTo(const QVector2D &to)
    {
        m_subPathToStart = true;
        m_currentPoint = to;
    }

    void lineTo(const QVector2D &to)
    {
        addElement({}, to, true);
    }

    void quadTo(const QVector2D &control, const QVector2D &to)
    {
        addElement(control, to);
    }

    Element &elementAt(int i)
    {
        return i < 0 ? m_childElements[-(i + 1)] : m_elements[i];
    }

    const Element &elementAt(int i) const
    {
        return i < 0 ? m_childElements[-(i + 1)] : m_elements[i];
    }

    int indexOfChildAt(int i, int childNumber) const
    {
        return elementAt(i).indexOfChild(childNumber);
    }

    QRectF controlPointRect() const;

    Qt::FillRule fillRule() const { return m_windingFill ? Qt::WindingFill : Qt::OddEvenFill; }
    void setFillRule(Qt::FillRule rule) { m_windingFill = (rule == Qt::WindingFill); }

    void reserve(int size) { m_elements.reserve(size); }
    int elementCount() const { return m_elements.size(); }
    bool isEmpty() const { return m_elements.size() == 0; }
    int elementCountRecursive() const;

    static QQuadPath fromPainterPath(const QPainterPath &path, PathHints hints = {});
    QPainterPath toPainterPath() const;
    QString asSvgString() const;

    QQuadPath subPathsClosed(bool *didClose = nullptr) const;
    void addCurvatureData();
    QQuadPath flattened() const;
    QQuadPath dashed(qreal lineWidth, const QList<qreal> &dashPattern, qreal dashOffset = 0) const;
    void splitElementAt(int index);
    bool contains(const QVector2D &point) const;
    bool contains(const QVector2D &point, int fromIndex, int toIndex) const;
    Element::FillSide fillSideOf(int elementIdx, float elementT) const;

    template<typename Func>
    void iterateChildrenOf(Element &e, Func &&lambda)
    {
        const int lastChildIndex = e.m_firstChildIndex + e.childCount() - 1;
        for (int i = e.m_firstChildIndex; i <= lastChildIndex; i++) {
            Element &c = m_childElements[i];
            if (c.childCount() > 0)
                iterateChildrenOf(c, lambda);
            else
                lambda(c, -(i + 1));
        }
    }

    template<typename Func>
    void iterateChildrenOf(const Element &e, Func &&lambda) const
    {
        const int lastChildIndex = e.m_firstChildIndex + e.childCount() - 1;
        for (int i = e.m_firstChildIndex; i <= lastChildIndex; i++) {
            const Element &c = m_childElements[i];
            if (c.childCount() > 0)
                iterateChildrenOf(c, lambda);
            else
                lambda(c, -(i + 1));
        }
    }

    template<typename Func>
    void iterateElements(Func &&lambda)
    {
        for (int i = 0; i < m_elements.size(); i++) {
            Element &e = m_elements[i];
            if (e.childCount() > 0)
                iterateChildrenOf(e, lambda);
            else
                lambda(e, i);
        }
    }

    template<typename Func>
    void iterateElements(Func &&lambda) const
    {
        for (int i = 0; i < m_elements.size(); i++) {
            const Element &e = m_elements[i];
            if (e.childCount() > 0)
                iterateChildrenOf(e, lambda);
            else
                lambda(e, i);
        }
    }

    static QVector2D closestPointOnLine(const QVector2D &p, const QVector2D &sp, const QVector2D &ep);
    static bool isPointOnLeft(const QVector2D &p, const QVector2D &sp, const QVector2D &ep);
    static bool isPointOnLine(const QVector2D &p, const QVector2D &sp, const QVector2D &ep);
    static bool isPointNearLine(const QVector2D &p, const QVector2D &sp, const QVector2D &ep);

    bool testHint(PathHint hint) const
    {
        return m_hints.testFlag(hint);
    }

    void setHint(PathHint hint, bool on = true)
    {
        m_hints.setFlag(hint, on);
    }

    PathHints pathHints() const
    {
        return m_hints;
    }

    void setPathHints(PathHints newHints)
    {
        m_hints = newHints;
    }

private:
    void addElement(const QVector2D &control, const QVector2D &to, bool isLine = false);
    void addElement(const Element &e);
    Element::CurvatureFlags coordinateOrderOfElement(const Element &element) const;

    friend Q_QUICK_EXPORT QDebug operator<<(QDebug, const QQuadPath &);

    QList<Element> m_elements;
    QList<Element> m_childElements;
    QVector2D m_currentPoint;
    bool m_subPathToStart = true;
    bool m_windingFill = false;
    PathHints m_hints;

    friend class QSGCurveProcessor;
};

Q_DECLARE_OPERATORS_FOR_FLAGS(QQuadPath::PathHints);

Q_QUICK_EXPORT QDebug operator<<(QDebug, const QQuadPath::Element &);
Q_QUICK_EXPORT QDebug operator<<(QDebug, const QQuadPath &);

QT_END_NAMESPACE

#endif