Coverage Report

Created: 2025-07-16 07:53

/src/qtbase/src/gui/painting/qpainterpath_p.h
Line
Count
Source (jump to first uncovered line)
1
// Copyright (C) 2016 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
#ifndef QPAINTERPATH_P_H
5
#define QPAINTERPATH_P_H
6
7
//
8
//  W A R N I N G
9
//  -------------
10
//
11
// This file is not part of the Qt API.  It exists for the convenience
12
// of other Qt classes.  This header file may change from version to
13
// version without notice, or even be removed.
14
//
15
// We mean it.
16
//
17
18
#include <QtGui/private/qtguiglobal_p.h>
19
#include "QtGui/qpainterpath.h"
20
#include "QtGui/qregion.h"
21
#include "QtCore/qlist.h"
22
#include "QtCore/qshareddata.h"
23
#include "QtCore/qvarlengtharray.h"
24
25
#include <private/qvectorpath_p.h>
26
#include <private/qstroker_p.h>
27
#include <private/qbezier_p.h>
28
29
#include <memory>
30
31
QT_BEGIN_NAMESPACE
32
33
class QPolygonF;
34
class QVectorPathConverter;
35
36
class QVectorPathConverter
37
{
38
public:
39
    QVectorPathConverter(const QList<QPainterPath::Element> &path, bool hasWindingFill, bool convex)
40
0
        : pathData(path, hasWindingFill, convex),
41
0
          path(pathData.points.data(), path.size(), pathData.elements.data(), pathData.flags)
42
0
    {
43
0
    }
44
45
0
    const QVectorPath &vectorPath() {
46
0
        return path;
47
0
    }
48
49
    struct QVectorPathData {
50
        QVectorPathData(const QList<QPainterPath::Element> &path, bool hasWindingFill, bool convex)
51
0
            : elements(path.size()), points(path.size() * 2), flags(0)
52
0
        {
53
0
            int ptsPos = 0;
54
0
            bool isLines = true;
55
0
            for (int i=0; i<path.size(); ++i) {
56
0
                const QPainterPath::Element &e = path.at(i);
57
0
                elements[i] = e.type;
58
0
                points[ptsPos++] = e.x;
59
0
                points[ptsPos++] = e.y;
60
0
                if (e.type == QPainterPath::CurveToElement)
61
0
                    flags |= QVectorPath::CurvedShapeMask;
62
63
                // This is to check if the path contains only alternating lineTo/moveTo,
64
                // in which case we can set the LinesHint in the path. MoveTo is 0 and
65
                // LineTo is 1 so the i%2 gets us what we want cheaply.
66
0
                isLines = isLines && e.type == (QPainterPath::ElementType) (i%2);
67
0
            }
68
69
0
            if (hasWindingFill)
70
0
                flags |= QVectorPath::WindingFill;
71
0
            else
72
0
                flags |= QVectorPath::OddEvenFill;
73
74
0
            if (isLines)
75
0
                flags |= QVectorPath::LinesShapeMask;
76
0
            else {
77
0
                flags |= QVectorPath::AreaShapeMask;
78
0
                if (!convex)
79
0
                    flags |= QVectorPath::NonConvexShapeMask;
80
0
            }
81
82
0
        }
83
        QVarLengthArray<QPainterPath::ElementType> elements;
84
        QVarLengthArray<qreal> points;
85
        uint flags;
86
    };
87
88
    QVectorPathData pathData;
89
    QVectorPath path;
90
91
private:
92
    Q_DISABLE_COPY_MOVE(QVectorPathConverter)
93
};
94
95
class QPainterPathPrivate : public QSharedData
96
{
97
public:
98
    friend class QPainterPath;
99
    friend class QPainterPathStroker;
100
    friend class QPainterPathStrokerPrivate;
101
    friend class QTransform;
102
    friend class QVectorPath;
103
#ifndef QT_NO_DATASTREAM
104
    friend Q_GUI_EXPORT QDataStream &operator<<(QDataStream &, const QPainterPath &);
105
    friend Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QPainterPath &);
106
#endif
107
108
    QPainterPathPrivate() noexcept
109
0
        : QSharedData(),
110
0
          require_moveTo(false),
111
0
          dirtyBounds(false),
112
0
          dirtyControlBounds(false),
113
0
          convex(false),
114
0
          hasWindingFill(false),
115
0
          cacheEnabled(false)
116
0
    {
117
0
    }
118
119
    QPainterPathPrivate(QPointF startPoint)
120
0
        : QSharedData(),
121
0
          elements{ { startPoint.x(), startPoint.y(), QPainterPath::MoveToElement } },
122
0
          bounds(startPoint, QSizeF(0, 0)),
123
0
          controlBounds(startPoint, QSizeF(0, 0)),
124
0
          require_moveTo(false),
125
0
          dirtyBounds(false),
126
0
          dirtyControlBounds(false),
127
0
          convex(false),
128
0
          hasWindingFill(false),
129
0
          cacheEnabled(false)
130
0
    {
131
0
    }
132
133
    QPainterPathPrivate(const QPainterPathPrivate &other) noexcept
134
0
        : QSharedData(other),
135
0
          elements(other.elements),
136
0
          m_runLengths(other.m_runLengths),
137
0
          bounds(other.bounds),
138
0
          controlBounds(other.controlBounds),
139
0
          cStart(other.cStart),
140
0
          require_moveTo(false),
141
0
          dirtyBounds(other.dirtyBounds),
142
0
          dirtyControlBounds(other.dirtyControlBounds),
143
0
          dirtyRunLengths(other.dirtyRunLengths),
144
0
          convex(other.convex),
145
0
          hasWindingFill(other.hasWindingFill),
146
0
          cacheEnabled(other.cacheEnabled)
147
0
    {
148
0
    }
149
150
    QPainterPathPrivate &operator=(const QPainterPathPrivate &) = delete;
151
0
    ~QPainterPathPrivate() = default;
152
153
    inline bool isClosed() const;
154
    inline void close();
155
    inline void maybeMoveTo();
156
    inline void clear();
157
    QPointF endPointOfElement(int elemIdx) const;
158
    void computeRunLengths();
159
    int elementAtLength(qreal len);
160
    int elementAtT(qreal t);
161
    QBezier bezierAtT(const QPainterPath &path, qreal t, qreal *startingLength,
162
                      qreal *bezierLength) const;
163
    enum TrimFlags {
164
        TrimStart = 0x01,
165
        TrimEnd = 0x02
166
    };
167
    void appendTrimmedElement(QPainterPath *to, int elemIdx, int trimFlags, qreal startLen, qreal endLen);
168
    void appendStartOfElement(QPainterPath *to, int elemIdx, qreal len)
169
0
    {
170
0
        appendTrimmedElement(to, elemIdx, TrimEnd, 0, len);
171
0
    }
172
    void appendEndOfElement(QPainterPath *to, int elemIdx, qreal len)
173
0
    {
174
0
        appendTrimmedElement(to, elemIdx, TrimStart, len, 0);
175
0
    }
176
    void appendSliceOfElement(QPainterPath *to, int elemIdx, qreal fromLen, qreal toLen)
177
0
    {
178
0
        appendTrimmedElement(to, elemIdx, TrimStart | TrimEnd, fromLen, toLen);
179
0
    }
180
    void appendElementRange(QPainterPath *to, int first, int last);
181
182
0
    const QVectorPath &vectorPath() {
183
0
        if (!pathConverter)
184
0
            pathConverter.reset(new QVectorPathConverter(elements, hasWindingFill, convex));
185
0
        return pathConverter->path;
186
0
    }
187
188
private:
189
    QList<QPainterPath::Element> elements;
190
    std::unique_ptr<QVectorPathConverter> pathConverter;
191
    QList<qreal> m_runLengths;
192
    QRectF bounds;
193
    QRectF controlBounds;
194
195
    int cStart = 0;
196
197
    bool require_moveTo : 1;
198
    bool dirtyBounds : 1;
199
    bool dirtyControlBounds : 1;
200
    bool dirtyRunLengths : 1;
201
    bool convex : 1;
202
    bool hasWindingFill : 1;
203
    bool cacheEnabled : 1;
204
};
205
206
class QPainterPathStrokerPrivate
207
{
208
public:
209
    QPainterPathStrokerPrivate();
210
211
    QStroker stroker;
212
    QList<qfixed> dashPattern;
213
    qreal dashOffset;
214
};
215
216
inline const QPainterPath QVectorPath::convertToPainterPath() const
217
0
{
218
0
        QPainterPath path;
219
0
        path.ensureData();
220
0
        QPainterPathPrivate *data = path.d_func();
221
0
        data->elements.reserve(m_count);
222
0
        int index = 0;
223
0
        data->elements[0].x = m_points[index++];
224
0
        data->elements[0].y = m_points[index++];
225
226
0
        if (m_elements) {
227
0
            data->elements[0].type = m_elements[0];
228
0
            for (int i=1; i<m_count; ++i) {
229
0
                QPainterPath::Element element;
230
0
                element.x = m_points[index++];
231
0
                element.y = m_points[index++];
232
0
                element.type = m_elements[i];
233
0
                data->elements << element;
234
0
            }
235
0
        } else {
236
0
            data->elements[0].type = QPainterPath::MoveToElement;
237
0
            for (int i=1; i<m_count; ++i) {
238
0
                QPainterPath::Element element;
239
0
                element.x = m_points[index++];
240
0
                element.y = m_points[index++];
241
0
                element.type = QPainterPath::LineToElement;
242
0
                data->elements << element;
243
0
            }
244
0
        }
245
246
0
        data->hasWindingFill = !(m_hints & OddEvenFill);
247
0
        return path;
248
0
}
249
250
void Q_GUI_EXPORT qt_find_ellipse_coords(const QRectF &r, qreal angle, qreal length,
251
                                         QPointF* startPoint, QPointF *endPoint);
252
253
inline bool QPainterPathPrivate::isClosed() const
254
0
{
255
0
    const QPainterPath::Element &first = elements.at(cStart);
256
0
    const QPainterPath::Element &last = elements.last();
257
0
    return first.x == last.x && first.y == last.y;
258
0
}
259
260
inline void QPainterPathPrivate::close()
261
0
{
262
0
    Q_ASSERT(ref.loadRelaxed() == 1);
263
0
    require_moveTo = true;
264
0
    const QPainterPath::Element &first = elements.at(cStart);
265
0
    QPainterPath::Element &last = elements.last();
266
0
    if (first.x != last.x || first.y != last.y) {
267
0
        if (qFuzzyCompare(first.x, last.x) && qFuzzyCompare(first.y, last.y)) {
268
0
            last.x = first.x;
269
0
            last.y = first.y;
270
0
        } else {
271
0
            QPainterPath::Element e = { first.x, first.y, QPainterPath::LineToElement };
272
0
            elements << e;
273
0
        }
274
0
    }
275
0
}
276
277
inline void QPainterPathPrivate::maybeMoveTo()
278
0
{
279
0
    if (require_moveTo) {
280
0
        QPainterPath::Element e = elements.last();
281
0
        e.type = QPainterPath::MoveToElement;
282
0
        elements.append(e);
283
0
        require_moveTo = false;
284
0
    }
285
0
}
286
287
inline void QPainterPathPrivate::clear()
288
0
{
289
0
    Q_ASSERT(ref.loadRelaxed() == 1);
290
291
0
    elements.clear();
292
0
    m_runLengths.clear();
293
294
0
    cStart = 0;
295
0
    bounds = {};
296
0
    controlBounds = {};
297
298
0
    require_moveTo = false;
299
0
    dirtyBounds = false;
300
0
    dirtyControlBounds = false;
301
0
    dirtyRunLengths = false;
302
0
    convex = false;
303
304
0
    pathConverter.reset();
305
0
}
306
307
inline QPointF QPainterPathPrivate::endPointOfElement(int elemIdx) const
308
0
{
309
0
    const QPainterPath::Element &e = elements.at(elemIdx);
310
0
    if (e.isCurveTo())
311
0
        return elements.at(elemIdx + 2);
312
0
    else
313
0
        return e;
314
0
}
315
316
inline int QPainterPathPrivate::elementAtLength(qreal len)
317
0
{
318
0
    Q_ASSERT(cacheEnabled);
319
0
    Q_ASSERT(!dirtyRunLengths);
320
0
    const auto it = std::lower_bound(m_runLengths.constBegin(), m_runLengths.constEnd(), len);
321
0
    return (it == m_runLengths.constEnd()) ? m_runLengths.size() - 1 : int(it - m_runLengths.constBegin());
322
0
}
323
324
inline int QPainterPathPrivate::elementAtT(qreal t)
325
0
{
326
0
    Q_ASSERT(cacheEnabled);
327
0
    if (dirtyRunLengths)
328
0
        computeRunLengths();
329
0
    qreal len = t * m_runLengths.constLast();
330
0
    return elementAtLength(len);
331
0
}
332
333
0
#define KAPPA qreal(0.5522847498)
334
335
QT_END_NAMESPACE
336
337
#endif // QPAINTERPATH_P_H