Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qpainterpath_p.h
Go to the documentation of this file.
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
31QT_BEGIN_NAMESPACE
32
33class QPolygonF;
35
37{
38public:
39 QVectorPathConverter(const QList<QPainterPath::Element> &path, bool hasWindingFill, bool convex)
42 {
43 }
44
46 return path;
47 }
48
50 QVectorPathData(const QList<QPainterPath::Element> &path, bool hasWindingFill, bool convex)
51 : elements(path.size()), points(path.size() * 2), flags(0)
52 {
53 int ptsPos = 0;
54 bool isLines = true;
55 for (int i=0; i<path.size(); ++i) {
56 const QPainterPath::Element &e = path.at(i);
57 elements[i] = e.type;
58 points[ptsPos++] = e.x;
59 points[ptsPos++] = e.y;
60 if (e.type == QPainterPath::CurveToElement)
61 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 isLines = isLines && e.type == (QPainterPath::ElementType) (i%2);
67 }
68
69 if (hasWindingFill)
70 flags |= QVectorPath::WindingFill;
71 else
72 flags |= QVectorPath::OddEvenFill;
73
74 if (isLines)
75 flags |= QVectorPath::LinesShapeMask;
76 else {
77 flags |= QVectorPath::AreaShapeMask;
78 if (!convex)
79 flags |= QVectorPath::NonConvexShapeMask;
80 }
81
82 }
85 uint flags;
86 };
87
90
91private:
93};
94
96{
97public:
98 friend class QPainterPath;
99 friend class QPainterPathStroker;
101 friend class QTransform;
102 friend class QVectorPath;
103#ifndef QT_NO_DATASTREAM
106#endif
107
109 : require_moveTo(false),
110 dirtyBounds(false),
111 dirtyControlBounds(false),
112 convex(false),
113 hasWindingFill(false),
114 cacheEnabled(false)
115 {
116 }
117
118 QPainterPathPrivate(QPointF startPoint)
120 bounds(startPoint, QSizeF(0, 0)),
122 require_moveTo(false),
123 dirtyBounds(false),
124 dirtyControlBounds(false),
125 convex(false),
126 hasWindingFill(false),
127 cacheEnabled(false)
128 {
129 }
130
136 cStart(other.cStart),
137 require_moveTo(false),
138 dirtyBounds(other.dirtyBounds),
139 dirtyControlBounds(other.dirtyControlBounds),
140 dirtyRunLengths(other.dirtyRunLengths),
141 convex(other.convex),
142 hasWindingFill(other.hasWindingFill),
143 cacheEnabled(other.cacheEnabled)
144 {
145 }
146
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;
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 {
167 appendTrimmedElement(to, elemIdx, TrimEnd, 0, len);
168 }
169 void appendEndOfElement(QPainterPath *to, int elemIdx, qreal len)
170 {
171 appendTrimmedElement(to, elemIdx, TrimStart, len, 0);
172 }
173 void appendSliceOfElement(QPainterPath *to, int elemIdx, qreal fromLen, qreal toLen)
174 {
175 appendTrimmedElement(to, elemIdx, TrimStart | TrimEnd, fromLen, toLen);
176 }
177 void appendElementRange(QPainterPath *to, int first, int last);
178
180 if (!pathConverter)
181 pathConverter.reset(new QVectorPathConverter(elements, hasWindingFill, convex));
182 return pathConverter->path;
183 }
184
185private:
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
212
213inline const QPainterPath QVectorPath::convertToPainterPath() const
214{
215 QPainterPath path;
216 path.ensureData();
217 QPainterPathPrivate *data = path.d_func();
218 data->elements.reserve(m_count);
219 int index = 0;
220 data->elements[0].x = m_points[index++];
221 data->elements[0].y = m_points[index++];
222
223 if (m_elements) {
224 data->elements[0].type = m_elements[0];
225 for (int i=1; i<m_count; ++i) {
226 QPainterPath::Element element;
227 element.x = m_points[index++];
228 element.y = m_points[index++];
229 element.type = m_elements[i];
230 data->elements << element;
231 }
232 } else {
233 data->elements[0].type = QPainterPath::MoveToElement;
234 for (int i=1; i<m_count; ++i) {
235 QPainterPath::Element element;
236 element.x = m_points[index++];
237 element.y = m_points[index++];
238 element.type = QPainterPath::LineToElement;
239 data->elements << element;
240 }
241 }
242
243 data->hasWindingFill = !(m_hints & OddEvenFill);
244 return path;
245}
246
247void Q_GUI_EXPORT qt_find_ellipse_coords(const QRectF &r, qreal angle, qreal length,
248 QPointF* startPoint, QPointF *endPoint);
249
250inline bool QPainterPathPrivate::isClosed() const
251{
252 const QPainterPath::Element &first = elements.at(cStart);
253 const QPainterPath::Element &last = elements.last();
254 return first.x == last.x && first.y == last.y;
255}
256
258{
259 require_moveTo = true;
260 const QPainterPath::Element &first = elements.at(cStart);
261 QPainterPath::Element &last = elements.last();
262 if (first.x != last.x || first.y != last.y) {
263 if (qFuzzyCompare(QPointF(first), QPointF(last))) {
264 last.x = first.x;
265 last.y = first.y;
266 } else {
267 QPainterPath::Element e = { first.x, first.y, QPainterPath::LineToElement };
268 elements << e;
269 }
270 }
271}
272
274{
275 if (require_moveTo) {
276 QPainterPath::Element e = elements.last();
277 e.type = QPainterPath::MoveToElement;
278 elements.append(e);
279 require_moveTo = false;
280 }
281}
282
284{
285 elements.clear();
286 m_runLengths.clear();
287
288 cStart = 0;
289 bounds = {};
290 controlBounds = {};
291
292 require_moveTo = false;
293 dirtyBounds = false;
294 dirtyControlBounds = false;
295 dirtyRunLengths = false;
296 convex = false;
297
298 pathConverter.reset();
299}
300
302{
303 const QPainterPath::Element &e = elements.at(elemIdx);
304 if (e.isCurveTo())
305 return elements.at(elemIdx + 2);
306 else
307 return e;
308}
309
311{
312 Q_ASSERT(cacheEnabled);
313 Q_ASSERT(!dirtyRunLengths);
314 const auto it = std::lower_bound(m_runLengths.constBegin(), m_runLengths.constEnd(), len);
315 return (it == m_runLengths.constEnd()) ? m_runLengths.size() - 1 : int(it - m_runLengths.constBegin());
316}
317
318inline int QPainterPathPrivate::elementAtT(qreal t)
319{
320 Q_ASSERT(cacheEnabled);
321 if (dirtyRunLengths)
323 qreal len = t * m_runLengths.constLast();
324 return elementAtLength(len);
325}
326
327#define KAPPA qreal(0.5522847498)
328
329QT_END_NAMESPACE
330
331#endif // QPAINTERPATH_P_H
QTransform m_transform
QT_FT_Outline * outline()
void clipElements(const QPointF *points, const QPainterPath::ElementType *types, int count)
QDataBuffer< QT_FT_Vector > m_points
void convertElements(const QPointF *points, const QPainterPath::ElementType *types, int count)
void setMatrix(const QTransform &m)
Sets up the matrix to be used for conversion.
void moveTo(const QPointF &pt)
void curveTo(const QPointF &cp1, const QPointF &cp2, const QPointF &ep)
void setClipRect(QRect clipRect)
QDataBuffer< QPointF > m_elements
QPainterPath::ElementType * elementTypes() const
QDataBuffer< char > m_tags
QDataBuffer< int > m_contours
QDataBuffer< QPainterPath::ElementType > m_element_types
QT_FT_Outline m_outline
QT_FT_Outline * convertPath(const QPainterPath &path)
void beginOutline(Qt::FillRule fillRule)
void lineTo(const QPointF &pt)
int elementAtT(qreal t)
QPainterPathPrivate(QPointF startPoint)
void appendStartOfElement(QPainterPath *to, int elemIdx, qreal len)
int elementAtLength(qreal len)
void appendEndOfElement(QPainterPath *to, int elemIdx, qreal len)
QPainterPathPrivate(const QPainterPathPrivate &other) noexcept
void appendTrimmedElement(QPainterPath *to, int elemIdx, int trimFlags, qreal startLen, qreal endLen)
void appendSliceOfElement(QPainterPath *to, int elemIdx, qreal fromLen, qreal toLen)
QPainterPathPrivate & operator=(const QPainterPathPrivate &)=delete
const QVectorPath & vectorPath()
void appendElementRange(QPainterPath *to, int first, int last)
QBezier bezierAtT(const QPainterPath &path, qreal t, qreal *startingLength, qreal *bezierLength) const
QPointF endPointOfElement(int elemIdx) const
~QPainterPathPrivate()=default
QPainterPathPrivate() noexcept
friend class QPaintEngineExPrivate
Definition qpainter.h:439
QVectorPathConverter(const QList< QPainterPath::Element > &path, bool hasWindingFill, bool convex)
QVectorPathData pathData
const QVectorPath & vectorPath()
Combined button and popup list for selecting options.
#define qreal_to_fixed_26_6(f)
Q_GUI_EXPORT bool qt_scaleForTransform(const QTransform &transform, qreal *scale)
QT_BEGIN_NAMESPACE constexpr int QT_RASTER_COORD_LIMIT
QDebug Q_GUI_EXPORT & operator<<(QDebug &s, const QVectorPath &path)
QVectorPathData(const QList< QPainterPath::Element > &path, bool hasWindingFill, bool convex)
QVarLengthArray< QPainterPath::ElementType > elements