/src/qtbase/src/gui/painting/qpainterpath_p.h
Line | Count | Source |
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 | | // Qt-Security score:significant reason:default |
4 | | |
5 | | #ifndef QPAINTERPATH_P_H |
6 | | #define QPAINTERPATH_P_H |
7 | | |
8 | | // |
9 | | // W A R N I N G |
10 | | // ------------- |
11 | | // |
12 | | // This file is not part of the Qt API. It exists for the convenience |
13 | | // of other Qt classes. This header file may change from version to |
14 | | // version without notice, or even be removed. |
15 | | // |
16 | | // We mean it. |
17 | | // |
18 | | |
19 | | #include <QtGui/private/qtguiglobal_p.h> |
20 | | #include "QtGui/qpainterpath.h" |
21 | | #include "QtGui/qregion.h" |
22 | | #include "QtCore/qlist.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 |
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 | : require_moveTo(false), |
110 | 0 | dirtyBounds(false), |
111 | 0 | dirtyControlBounds(false), |
112 | 0 | convex(false), |
113 | 0 | hasWindingFill(false), |
114 | 0 | cacheEnabled(false) |
115 | 0 | { |
116 | 0 | } |
117 | | |
118 | | QPainterPathPrivate(QPointF startPoint) |
119 | 0 | : elements{ { startPoint.x(), startPoint.y(), QPainterPath::MoveToElement } }, |
120 | 0 | bounds(startPoint, QSizeF(0, 0)), |
121 | 0 | controlBounds(startPoint, QSizeF(0, 0)), |
122 | 0 | require_moveTo(false), |
123 | 0 | dirtyBounds(false), |
124 | 0 | dirtyControlBounds(false), |
125 | 0 | convex(false), |
126 | 0 | hasWindingFill(false), |
127 | 0 | cacheEnabled(false) |
128 | 0 | { |
129 | 0 | } |
130 | | |
131 | | QPainterPathPrivate(const QPainterPathPrivate &other) noexcept |
132 | 0 | : elements(other.elements), |
133 | 0 | m_runLengths(other.m_runLengths), |
134 | 0 | bounds(other.bounds), |
135 | 0 | controlBounds(other.controlBounds), |
136 | 0 | cStart(other.cStart), |
137 | 0 | require_moveTo(false), |
138 | 0 | dirtyBounds(other.dirtyBounds), |
139 | 0 | dirtyControlBounds(other.dirtyControlBounds), |
140 | 0 | dirtyRunLengths(other.dirtyRunLengths), |
141 | 0 | convex(other.convex), |
142 | 0 | hasWindingFill(other.hasWindingFill), |
143 | 0 | cacheEnabled(other.cacheEnabled) |
144 | 0 | { |
145 | 0 | } |
146 | | |
147 | | QPainterPathPrivate &operator=(const QPainterPathPrivate &) = delete; |
148 | 0 | ~QPainterPathPrivate() = default; |
149 | | |
150 | | inline bool isClosed() const; |
151 | | inline void close(); |
152 | | inline void maybeMoveTo(); |
153 | | inline void clear(); |
154 | | QPointF endPointOfElement(int elemIdx) const; |
155 | | void computeRunLengths(); |
156 | | int elementAtLength(qreal len); |
157 | | int elementAtT(qreal t); |
158 | | QBezier bezierAtT(const QPainterPath &path, qreal t, qreal *startingLength, |
159 | | qreal *bezierLength) const; |
160 | | enum TrimFlags { |
161 | | TrimStart = 0x01, |
162 | | TrimEnd = 0x02 |
163 | | }; |
164 | | void appendTrimmedElement(QPainterPath *to, int elemIdx, int trimFlags, qreal startLen, qreal endLen); |
165 | | void appendStartOfElement(QPainterPath *to, int elemIdx, qreal len) |
166 | 0 | { |
167 | 0 | appendTrimmedElement(to, elemIdx, TrimEnd, 0, len); |
168 | 0 | } |
169 | | void appendEndOfElement(QPainterPath *to, int elemIdx, qreal len) |
170 | 0 | { |
171 | 0 | appendTrimmedElement(to, elemIdx, TrimStart, len, 0); |
172 | 0 | } |
173 | | void appendSliceOfElement(QPainterPath *to, int elemIdx, qreal fromLen, qreal toLen) |
174 | 0 | { |
175 | 0 | appendTrimmedElement(to, elemIdx, TrimStart | TrimEnd, fromLen, toLen); |
176 | 0 | } |
177 | | void appendElementRange(QPainterPath *to, int first, int last); |
178 | | |
179 | 0 | const QVectorPath &vectorPath() { |
180 | 0 | if (!pathConverter) |
181 | 0 | pathConverter.reset(new QVectorPathConverter(elements, hasWindingFill, convex)); |
182 | 0 | return pathConverter->path; |
183 | 0 | } |
184 | | |
185 | | private: |
186 | | QList<QPainterPath::Element> elements; |
187 | | std::unique_ptr<QVectorPathConverter> pathConverter; |
188 | | QList<qreal> m_runLengths; |
189 | | QRectF bounds; |
190 | | QRectF controlBounds; |
191 | | |
192 | | int cStart = 0; |
193 | | |
194 | | bool require_moveTo : 1; |
195 | | bool dirtyBounds : 1; |
196 | | bool dirtyControlBounds : 1; |
197 | | bool dirtyRunLengths : 1; |
198 | | bool convex : 1; |
199 | | bool hasWindingFill : 1; |
200 | | bool cacheEnabled : 1; |
201 | | }; |
202 | | |
203 | | class QPainterPathStrokerPrivate |
204 | | { |
205 | | public: |
206 | | QPainterPathStrokerPrivate(); |
207 | | |
208 | | QStroker stroker; |
209 | | QList<qfixed> dashPattern; |
210 | | qreal dashOffset; |
211 | | }; |
212 | | |
213 | | inline const QPainterPath QVectorPath::convertToPainterPath() const |
214 | 0 | { |
215 | 0 | QPainterPath path; |
216 | 0 | path.ensureData(); |
217 | 0 | QPainterPathPrivate *data = path.d_func(); |
218 | 0 | data->elements.reserve(m_count); |
219 | 0 | int index = 0; |
220 | 0 | data->elements[0].x = m_points[index++]; |
221 | 0 | data->elements[0].y = m_points[index++]; |
222 | |
|
223 | 0 | if (m_elements) { |
224 | 0 | data->elements[0].type = m_elements[0]; |
225 | 0 | for (int i=1; i<m_count; ++i) { |
226 | 0 | QPainterPath::Element element; |
227 | 0 | element.x = m_points[index++]; |
228 | 0 | element.y = m_points[index++]; |
229 | 0 | element.type = m_elements[i]; |
230 | 0 | data->elements << element; |
231 | 0 | } |
232 | 0 | } else { |
233 | 0 | data->elements[0].type = QPainterPath::MoveToElement; |
234 | 0 | for (int i=1; i<m_count; ++i) { |
235 | 0 | QPainterPath::Element element; |
236 | 0 | element.x = m_points[index++]; |
237 | 0 | element.y = m_points[index++]; |
238 | 0 | element.type = QPainterPath::LineToElement; |
239 | 0 | data->elements << element; |
240 | 0 | } |
241 | 0 | } |
242 | |
|
243 | 0 | data->hasWindingFill = !(m_hints & OddEvenFill); |
244 | 0 | return path; |
245 | 0 | } |
246 | | |
247 | | void Q_GUI_EXPORT qt_find_ellipse_coords(const QRectF &r, qreal angle, qreal length, |
248 | | QPointF* startPoint, QPointF *endPoint); |
249 | | |
250 | | inline bool QPainterPathPrivate::isClosed() const |
251 | 0 | { |
252 | 0 | const QPainterPath::Element &first = elements.at(cStart); |
253 | 0 | const QPainterPath::Element &last = elements.last(); |
254 | 0 | return first.x == last.x && first.y == last.y; |
255 | 0 | } |
256 | | |
257 | | inline void QPainterPathPrivate::close() |
258 | 0 | { |
259 | 0 | require_moveTo = true; |
260 | 0 | const QPainterPath::Element &first = elements.at(cStart); |
261 | 0 | QPainterPath::Element &last = elements.last(); |
262 | 0 | if (first.x != last.x || first.y != last.y) { |
263 | 0 | if (qFuzzyCompare(QPointF(first), QPointF(last))) { |
264 | 0 | last.x = first.x; |
265 | 0 | last.y = first.y; |
266 | 0 | } else { |
267 | 0 | QPainterPath::Element e = { first.x, first.y, QPainterPath::LineToElement }; |
268 | 0 | elements << e; |
269 | 0 | } |
270 | 0 | } |
271 | 0 | } |
272 | | |
273 | | inline void QPainterPathPrivate::maybeMoveTo() |
274 | 0 | { |
275 | 0 | if (require_moveTo) { |
276 | 0 | QPainterPath::Element e = elements.last(); |
277 | 0 | e.type = QPainterPath::MoveToElement; |
278 | 0 | elements.append(e); |
279 | 0 | require_moveTo = false; |
280 | 0 | } |
281 | 0 | } |
282 | | |
283 | | inline void QPainterPathPrivate::clear() |
284 | 0 | { |
285 | 0 | elements.clear(); |
286 | 0 | m_runLengths.clear(); |
287 | |
|
288 | 0 | cStart = 0; |
289 | 0 | bounds = {}; |
290 | 0 | controlBounds = {}; |
291 | |
|
292 | 0 | require_moveTo = false; |
293 | 0 | dirtyBounds = false; |
294 | 0 | dirtyControlBounds = false; |
295 | 0 | dirtyRunLengths = false; |
296 | 0 | convex = false; |
297 | |
|
298 | 0 | pathConverter.reset(); |
299 | 0 | } |
300 | | |
301 | | inline QPointF QPainterPathPrivate::endPointOfElement(int elemIdx) const |
302 | 0 | { |
303 | 0 | const QPainterPath::Element &e = elements.at(elemIdx); |
304 | 0 | if (e.isCurveTo()) |
305 | 0 | return elements.at(elemIdx + 2); |
306 | 0 | else |
307 | 0 | return e; |
308 | 0 | } |
309 | | |
310 | | inline int QPainterPathPrivate::elementAtLength(qreal len) |
311 | 0 | { |
312 | 0 | Q_ASSERT(cacheEnabled); |
313 | 0 | Q_ASSERT(!dirtyRunLengths); |
314 | 0 | const auto it = std::lower_bound(m_runLengths.constBegin(), m_runLengths.constEnd(), len); |
315 | 0 | return (it == m_runLengths.constEnd()) ? m_runLengths.size() - 1 : int(it - m_runLengths.constBegin()); |
316 | 0 | } |
317 | | |
318 | | inline int QPainterPathPrivate::elementAtT(qreal t) |
319 | 0 | { |
320 | 0 | Q_ASSERT(cacheEnabled); |
321 | 0 | if (dirtyRunLengths) |
322 | 0 | computeRunLengths(); |
323 | 0 | qreal len = t * m_runLengths.constLast(); |
324 | 0 | return elementAtLength(len); |
325 | 0 | } |
326 | | |
327 | 0 | #define KAPPA qreal(0.5522847498) |
328 | | |
329 | | QT_END_NAMESPACE |
330 | | |
331 | | #endif // QPAINTERPATH_P_H |