Coverage Report

Created: 2025-07-12 07:23

/src/qtbase/src/gui/painting/qpainterpath_p.h
Line
Count
Source (jump to first uncovered line)
1
/****************************************************************************
2
**
3
** Copyright (C) 2016 The Qt Company Ltd.
4
** Contact: https://www.qt.io/licensing/
5
**
6
** This file is part of the QtGui module of the Qt Toolkit.
7
**
8
** $QT_BEGIN_LICENSE:LGPL$
9
** Commercial License Usage
10
** Licensees holding valid commercial Qt licenses may use this file in
11
** accordance with the commercial license agreement provided with the
12
** Software or, alternatively, in accordance with the terms contained in
13
** a written agreement between you and The Qt Company. For licensing terms
14
** and conditions see https://www.qt.io/terms-conditions. For further
15
** information use the contact form at https://www.qt.io/contact-us.
16
**
17
** GNU Lesser General Public License Usage
18
** Alternatively, this file may be used under the terms of the GNU Lesser
19
** General Public License version 3 as published by the Free Software
20
** Foundation and appearing in the file LICENSE.LGPL3 included in the
21
** packaging of this file. Please review the following information to
22
** ensure the GNU Lesser General Public License version 3 requirements
23
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24
**
25
** GNU General Public License Usage
26
** Alternatively, this file may be used under the terms of the GNU
27
** General Public License version 2.0 or (at your option) the GNU General
28
** Public license version 3 or any later version approved by the KDE Free
29
** Qt Foundation. The licenses are as published by the Free Software
30
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31
** included in the packaging of this file. Please review the following
32
** information to ensure the GNU General Public License requirements will
33
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34
** https://www.gnu.org/licenses/gpl-3.0.html.
35
**
36
** $QT_END_LICENSE$
37
**
38
****************************************************************************/
39
40
#ifndef QPAINTERPATH_P_H
41
#define QPAINTERPATH_P_H
42
43
//
44
//  W A R N I N G
45
//  -------------
46
//
47
// This file is not part of the Qt API.  It exists for the convenience
48
// of other Qt classes.  This header file may change from version to
49
// version without notice, or even be removed.
50
//
51
// We mean it.
52
//
53
54
#include <QtGui/private/qtguiglobal_p.h>
55
#include "QtGui/qpainterpath.h"
56
#include "QtGui/qregion.h"
57
#include "QtCore/qlist.h"
58
#include "QtCore/qvarlengtharray.h"
59
60
#include <qdebug.h>
61
62
#include <private/qvectorpath_p.h>
63
#include <private/qstroker_p.h>
64
65
#include <memory>
66
67
QT_BEGIN_NAMESPACE
68
69
// ### Qt 6: merge with QPainterPathData
70
class QPainterPathPrivate
71
{
72
public:
73
    friend class QPainterPath;
74
    friend class QPainterPathData;
75
    friend class QPainterPathStroker;
76
    friend class QPainterPathStrokerPrivate;
77
    friend class QMatrix;
78
    friend class QTransform;
79
    friend class QVectorPath;
80
    friend struct QPainterPathPrivateDeleter;
81
#ifndef QT_NO_DATASTREAM
82
    friend Q_GUI_EXPORT QDataStream &operator<<(QDataStream &, const QPainterPath &);
83
    friend Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QPainterPath &);
84
#endif
85
86
    QPainterPathPrivate() noexcept
87
0
        : ref(1)
88
0
    {
89
0
    }
90
91
    QPainterPathPrivate(const QPainterPathPrivate &other) noexcept
92
0
        : ref(1),
93
0
          elements(other.elements)
94
0
    {
95
0
    }
96
97
    QPainterPathPrivate &operator=(const QPainterPathPrivate &) = delete;
98
0
    ~QPainterPathPrivate() = default;
99
100
private:
101
    QAtomicInt ref;
102
    QVector<QPainterPath::Element> elements;
103
};
104
105
class QPainterPathStrokerPrivate
106
{
107
public:
108
    QPainterPathStrokerPrivate();
109
110
    QStroker stroker;
111
    QVector<qfixed> dashPattern;
112
    qreal dashOffset;
113
};
114
115
class QPolygonF;
116
class QVectorPathConverter;
117
118
class QVectorPathConverter
119
{
120
public:
121
    QVectorPathConverter(const QVector<QPainterPath::Element> &path, uint fillRule, bool convex)
122
0
        : pathData(path, fillRule, convex),
123
0
          path(pathData.points.data(), path.size(),
124
0
               pathData.elements.data(), pathData.flags) {}
125
126
0
    const QVectorPath &vectorPath() {
127
0
        return path;
128
0
    }
129
130
    struct QVectorPathData {
131
        QVectorPathData(const QVector<QPainterPath::Element> &path, uint fillRule, bool convex)
132
0
            : elements(path.size()),
133
0
              points(path.size() * 2),
134
0
              flags(0)
135
0
        {
136
0
            int ptsPos = 0;
137
0
            bool isLines = true;
138
0
            for (int i=0; i<path.size(); ++i) {
139
0
                const QPainterPath::Element &e = path.at(i);
140
0
                elements[i] = e.type;
141
0
                points[ptsPos++] = e.x;
142
0
                points[ptsPos++] = e.y;
143
0
                if (e.type == QPainterPath::CurveToElement)
144
0
                    flags |= QVectorPath::CurvedShapeMask;
145
146
                // This is to check if the path contains only alternating lineTo/moveTo,
147
                // in which case we can set the LinesHint in the path. MoveTo is 0 and
148
                // LineTo is 1 so the i%2 gets us what we want cheaply.
149
0
                isLines = isLines && e.type == (QPainterPath::ElementType) (i%2);
150
0
            }
151
152
0
            if (fillRule == Qt::WindingFill)
153
0
                flags |= QVectorPath::WindingFill;
154
0
            else
155
0
                flags |= QVectorPath::OddEvenFill;
156
157
0
            if (isLines)
158
0
                flags |= QVectorPath::LinesShapeMask;
159
0
            else {
160
0
                flags |= QVectorPath::AreaShapeMask;
161
0
                if (!convex)
162
0
                    flags |= QVectorPath::NonConvexShapeMask;
163
0
            }
164
165
0
        }
166
        QVarLengthArray<QPainterPath::ElementType> elements;
167
        QVarLengthArray<qreal> points;
168
        uint flags;
169
    };
170
171
    QVectorPathData pathData;
172
    QVectorPath path;
173
174
private:
175
    Q_DISABLE_COPY_MOVE(QVectorPathConverter)
176
};
177
178
class QPainterPathData : public QPainterPathPrivate
179
{
180
public:
181
    QPainterPathData() :
182
0
        cStart(0),
183
0
        fillRule(Qt::OddEvenFill),
184
0
        require_moveTo(false),
185
0
        dirtyBounds(false),
186
0
        dirtyControlBounds(false),
187
0
        convex(false),
188
0
        pathConverter(nullptr)
189
0
    {
190
0
    }
191
192
    QPainterPathData(const QPainterPathData &other) :
193
0
        QPainterPathPrivate(other),
194
0
        cStart(other.cStart),
195
0
        fillRule(other.fillRule),
196
0
        bounds(other.bounds),
197
0
        controlBounds(other.controlBounds),
198
0
        require_moveTo(false),
199
0
        dirtyBounds(other.dirtyBounds),
200
0
        dirtyControlBounds(other.dirtyControlBounds),
201
0
        convex(other.convex),
202
0
        pathConverter(nullptr)
203
0
    {
204
0
    }
205
206
    QPainterPathData &operator=(const QPainterPathData &) = delete;
207
0
    ~QPainterPathData() = default;
208
209
    inline bool isClosed() const;
210
    inline void close();
211
    inline void maybeMoveTo();
212
    inline void clear();
213
214
0
    const QVectorPath &vectorPath() {
215
0
        if (!pathConverter)
216
0
            pathConverter.reset(new QVectorPathConverter(elements, fillRule, convex));
217
0
        return pathConverter->path;
218
0
    }
219
220
    int cStart;
221
    Qt::FillRule fillRule;
222
223
    QRectF bounds;
224
    QRectF controlBounds;
225
226
    uint require_moveTo : 1;
227
    uint dirtyBounds : 1;
228
    uint dirtyControlBounds : 1;
229
    uint convex : 1;
230
231
    std::unique_ptr<QVectorPathConverter> pathConverter;
232
};
233
234
235
inline const QPainterPath QVectorPath::convertToPainterPath() const
236
0
{
237
0
        QPainterPath path;
238
0
        path.ensureData();
239
0
        QPainterPathData *data = path.d_func();
240
0
        data->elements.reserve(m_count);
241
0
        int index = 0;
242
0
        data->elements[0].x = m_points[index++];
243
0
        data->elements[0].y = m_points[index++];
244
245
0
        if (m_elements) {
246
0
            data->elements[0].type = m_elements[0];
247
0
            for (int i=1; i<m_count; ++i) {
248
0
                QPainterPath::Element element;
249
0
                element.x = m_points[index++];
250
0
                element.y = m_points[index++];
251
0
                element.type = m_elements[i];
252
0
                data->elements << element;
253
0
            }
254
0
        } else {
255
0
            data->elements[0].type = QPainterPath::MoveToElement;
256
0
            for (int i=1; i<m_count; ++i) {
257
0
                QPainterPath::Element element;
258
0
                element.x = m_points[index++];
259
0
                element.y = m_points[index++];
260
0
                element.type = QPainterPath::LineToElement;
261
0
                data->elements << element;
262
0
            }
263
0
        }
264
265
0
        if (m_hints & OddEvenFill)
266
0
            data->fillRule = Qt::OddEvenFill;
267
0
        else
268
0
            data->fillRule = Qt::WindingFill;
269
0
        return path;
270
0
}
271
272
void Q_GUI_EXPORT qt_find_ellipse_coords(const QRectF &r, qreal angle, qreal length,
273
                                         QPointF* startPoint, QPointF *endPoint);
274
275
inline bool QPainterPathData::isClosed() const
276
0
{
277
0
    const QPainterPath::Element &first = elements.at(cStart);
278
0
    const QPainterPath::Element &last = elements.last();
279
0
    return first.x == last.x && first.y == last.y;
280
0
}
281
282
inline void QPainterPathData::close()
283
0
{
284
0
    Q_ASSERT(ref.loadRelaxed() == 1);
285
0
    require_moveTo = true;
286
0
    const QPainterPath::Element &first = elements.at(cStart);
287
0
    QPainterPath::Element &last = elements.last();
288
0
    if (first.x != last.x || first.y != last.y) {
289
0
        if (qFuzzyCompare(first.x, last.x) && qFuzzyCompare(first.y, last.y)) {
290
0
            last.x = first.x;
291
0
            last.y = first.y;
292
0
        } else {
293
0
            QPainterPath::Element e = { first.x, first.y, QPainterPath::LineToElement };
294
0
            elements << e;
295
0
        }
296
0
    }
297
0
}
298
299
inline void QPainterPathData::maybeMoveTo()
300
0
{
301
0
    if (require_moveTo) {
302
0
        QPainterPath::Element e = elements.last();
303
0
        e.type = QPainterPath::MoveToElement;
304
0
        elements.append(e);
305
0
        require_moveTo = false;
306
0
    }
307
0
}
308
309
inline void QPainterPathData::clear()
310
0
{
311
0
    Q_ASSERT(ref.loadRelaxed() == 1);
312
313
0
    elements.clear();
314
315
0
    cStart = 0;
316
0
    bounds = {};
317
0
    controlBounds = {};
318
319
0
    require_moveTo = false;
320
0
    dirtyBounds = false;
321
0
    dirtyControlBounds = false;
322
0
    convex = false;
323
324
0
    pathConverter.reset();
325
0
}
326
0
#define KAPPA qreal(0.5522847498)
327
328
329
QT_END_NAMESPACE
330
331
#endif // QPAINTERPATH_P_H