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 | : ref(1) |
88 | { |
89 | } |
90 | |
91 | QPainterPathPrivate(const QPainterPathPrivate &other) noexcept |
92 | : ref(1), |
93 | elements(other.elements) |
94 | { |
95 | } |
96 | |
97 | QPainterPathPrivate &operator=(const QPainterPathPrivate &) = delete; |
98 | ~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 | : pathData(path, fillRule, convex), |
123 | path(pathData.points.data(), path.size(), |
124 | pathData.elements.data(), pathData.flags) {} |
125 | |
126 | const QVectorPath &vectorPath() { |
127 | return path; |
128 | } |
129 | |
130 | struct QVectorPathData { |
131 | QVectorPathData(const QVector<QPainterPath::Element> &path, uint fillRule, bool convex) |
132 | : elements(path.size()), |
133 | points(path.size() * 2), |
134 | flags(0) |
135 | { |
136 | int ptsPos = 0; |
137 | bool isLines = true; |
138 | for (int i=0; i<path.size(); ++i) { |
139 | const QPainterPath::Element &e = path.at(i); |
140 | elements[i] = e.type; |
141 | points[ptsPos++] = e.x; |
142 | points[ptsPos++] = e.y; |
143 | if (e.type == QPainterPath::CurveToElement) |
144 | 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 | isLines = isLines && e.type == (QPainterPath::ElementType) (i%2); |
150 | } |
151 | |
152 | if (fillRule == Qt::WindingFill) |
153 | flags |= QVectorPath::WindingFill; |
154 | else |
155 | flags |= QVectorPath::OddEvenFill; |
156 | |
157 | if (isLines) |
158 | flags |= QVectorPath::LinesShapeMask; |
159 | else { |
160 | flags |= QVectorPath::AreaShapeMask; |
161 | if (!convex) |
162 | flags |= QVectorPath::NonConvexShapeMask; |
163 | } |
164 | |
165 | } |
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 | cStart(0), |
183 | fillRule(Qt::OddEvenFill), |
184 | require_moveTo(false), |
185 | dirtyBounds(false), |
186 | dirtyControlBounds(false), |
187 | convex(false), |
188 | pathConverter(nullptr) |
189 | { |
190 | } |
191 | |
192 | QPainterPathData(const QPainterPathData &other) : |
193 | QPainterPathPrivate(other), |
194 | cStart(other.cStart), |
195 | fillRule(other.fillRule), |
196 | bounds(other.bounds), |
197 | controlBounds(other.controlBounds), |
198 | require_moveTo(false), |
199 | dirtyBounds(other.dirtyBounds), |
200 | dirtyControlBounds(other.dirtyControlBounds), |
201 | convex(other.convex), |
202 | pathConverter(nullptr) |
203 | { |
204 | } |
205 | |
206 | QPainterPathData &operator=(const QPainterPathData &) = delete; |
207 | ~QPainterPathData() = default; |
208 | |
209 | inline bool isClosed() const; |
210 | inline void close(); |
211 | inline void maybeMoveTo(); |
212 | inline void clear(); |
213 | |
214 | const QVectorPath &vectorPath() { |
215 | if (!pathConverter) |
216 | pathConverter.reset(p: new QVectorPathConverter(elements, fillRule, convex)); |
217 | return pathConverter->path; |
218 | } |
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 | { |
237 | QPainterPath path; |
238 | path.ensureData(); |
239 | QPainterPathData *data = path.d_func(); |
240 | data->elements.reserve(asize: m_count); |
241 | int index = 0; |
242 | data->elements[0].x = m_points[index++]; |
243 | data->elements[0].y = m_points[index++]; |
244 | |
245 | if (m_elements) { |
246 | data->elements[0].type = m_elements[0]; |
247 | for (int i=1; i<m_count; ++i) { |
248 | QPainterPath::Element element; |
249 | element.x = m_points[index++]; |
250 | element.y = m_points[index++]; |
251 | element.type = m_elements[i]; |
252 | data->elements << element; |
253 | } |
254 | } else { |
255 | data->elements[0].type = QPainterPath::MoveToElement; |
256 | for (int i=1; i<m_count; ++i) { |
257 | QPainterPath::Element element; |
258 | element.x = m_points[index++]; |
259 | element.y = m_points[index++]; |
260 | element.type = QPainterPath::LineToElement; |
261 | data->elements << element; |
262 | } |
263 | } |
264 | |
265 | if (m_hints & OddEvenFill) |
266 | data->fillRule = Qt::OddEvenFill; |
267 | else |
268 | data->fillRule = Qt::WindingFill; |
269 | return path; |
270 | } |
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 | { |
277 | const QPainterPath::Element &first = elements.at(i: cStart); |
278 | const QPainterPath::Element &last = elements.last(); |
279 | return first.x == last.x && first.y == last.y; |
280 | } |
281 | |
282 | inline void QPainterPathData::close() |
283 | { |
284 | Q_ASSERT(ref.loadRelaxed() == 1); |
285 | require_moveTo = true; |
286 | const QPainterPath::Element &first = elements.at(i: cStart); |
287 | QPainterPath::Element &last = elements.last(); |
288 | if (first.x != last.x || first.y != last.y) { |
289 | if (qFuzzyCompare(p1: first.x, p2: last.x) && qFuzzyCompare(p1: first.y, p2: last.y)) { |
290 | last.x = first.x; |
291 | last.y = first.y; |
292 | } else { |
293 | QPainterPath::Element e = { .x: first.x, .y: first.y, .type: QPainterPath::LineToElement }; |
294 | elements << e; |
295 | } |
296 | } |
297 | } |
298 | |
299 | inline void QPainterPathData::maybeMoveTo() |
300 | { |
301 | if (require_moveTo) { |
302 | QPainterPath::Element e = elements.last(); |
303 | e.type = QPainterPath::MoveToElement; |
304 | elements.append(t: e); |
305 | require_moveTo = false; |
306 | } |
307 | } |
308 | |
309 | inline void QPainterPathData::clear() |
310 | { |
311 | Q_ASSERT(ref.loadRelaxed() == 1); |
312 | |
313 | elements.clear(); |
314 | |
315 | cStart = 0; |
316 | bounds = {}; |
317 | controlBounds = {}; |
318 | |
319 | require_moveTo = false; |
320 | dirtyBounds = false; |
321 | dirtyControlBounds = false; |
322 | convex = false; |
323 | |
324 | pathConverter.reset(); |
325 | } |
326 | #define KAPPA qreal(0.5522847498) |
327 | |
328 | |
329 | QT_END_NAMESPACE |
330 | |
331 | #endif // QPAINTERPATH_P_H |
332 | |