| 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 | class QPolygonF; | 
|---|
| 70 | class QVectorPathConverter; | 
|---|
| 71 |  | 
|---|
| 72 | class QVectorPathConverter | 
|---|
| 73 | { | 
|---|
| 74 | public: | 
|---|
| 75 | QVectorPathConverter(const QList<QPainterPath::Element> &path, uint fillRule, bool convex) | 
|---|
| 76 | : pathData(path, fillRule, convex), | 
|---|
| 77 | path(pathData.points.data(), path.size(), pathData.elements.data(), pathData.flags) | 
|---|
| 78 | { | 
|---|
| 79 | } | 
|---|
| 80 |  | 
|---|
| 81 | const QVectorPath &vectorPath() { | 
|---|
| 82 | return path; | 
|---|
| 83 | } | 
|---|
| 84 |  | 
|---|
| 85 | struct QVectorPathData { | 
|---|
| 86 | QVectorPathData(const QList<QPainterPath::Element> &path, uint fillRule, bool convex) | 
|---|
| 87 | : elements(path.size()), points(path.size() * 2), flags(0) | 
|---|
| 88 | { | 
|---|
| 89 | int ptsPos = 0; | 
|---|
| 90 | bool isLines = true; | 
|---|
| 91 | for (int i=0; i<path.size(); ++i) { | 
|---|
| 92 | const QPainterPath::Element &e = path.at(i); | 
|---|
| 93 | elements[i] = e.type; | 
|---|
| 94 | points[ptsPos++] = e.x; | 
|---|
| 95 | points[ptsPos++] = e.y; | 
|---|
| 96 | if (e.type == QPainterPath::CurveToElement) | 
|---|
| 97 | flags |= QVectorPath::CurvedShapeMask; | 
|---|
| 98 |  | 
|---|
| 99 | // This is to check if the path contains only alternating lineTo/moveTo, | 
|---|
| 100 | // in which case we can set the LinesHint in the path. MoveTo is 0 and | 
|---|
| 101 | // LineTo is 1 so the i%2 gets us what we want cheaply. | 
|---|
| 102 | isLines = isLines && e.type == (QPainterPath::ElementType) (i%2); | 
|---|
| 103 | } | 
|---|
| 104 |  | 
|---|
| 105 | if (fillRule == Qt::WindingFill) | 
|---|
| 106 | flags |= QVectorPath::WindingFill; | 
|---|
| 107 | else | 
|---|
| 108 | flags |= QVectorPath::OddEvenFill; | 
|---|
| 109 |  | 
|---|
| 110 | if (isLines) | 
|---|
| 111 | flags |= QVectorPath::LinesShapeMask; | 
|---|
| 112 | else { | 
|---|
| 113 | flags |= QVectorPath::AreaShapeMask; | 
|---|
| 114 | if (!convex) | 
|---|
| 115 | flags |= QVectorPath::NonConvexShapeMask; | 
|---|
| 116 | } | 
|---|
| 117 |  | 
|---|
| 118 | } | 
|---|
| 119 | QVarLengthArray<QPainterPath::ElementType> elements; | 
|---|
| 120 | QVarLengthArray<qreal> points; | 
|---|
| 121 | uint flags; | 
|---|
| 122 | }; | 
|---|
| 123 |  | 
|---|
| 124 | QVectorPathData pathData; | 
|---|
| 125 | QVectorPath path; | 
|---|
| 126 |  | 
|---|
| 127 | private: | 
|---|
| 128 | Q_DISABLE_COPY_MOVE(QVectorPathConverter) | 
|---|
| 129 | }; | 
|---|
| 130 |  | 
|---|
| 131 | class QPainterPathPrivate | 
|---|
| 132 | { | 
|---|
| 133 | public: | 
|---|
| 134 | friend class QPainterPath; | 
|---|
| 135 | friend class QPainterPathStroker; | 
|---|
| 136 | friend class QPainterPathStrokerPrivate; | 
|---|
| 137 | friend class QTransform; | 
|---|
| 138 | friend class QVectorPath; | 
|---|
| 139 | friend struct QPainterPathPrivateDeleter; | 
|---|
| 140 | #ifndef QT_NO_DATASTREAM | 
|---|
| 141 | friend Q_GUI_EXPORT QDataStream &operator<<(QDataStream &, const QPainterPath &); | 
|---|
| 142 | friend Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QPainterPath &); | 
|---|
| 143 | #endif | 
|---|
| 144 |  | 
|---|
| 145 | QPainterPathPrivate() noexcept | 
|---|
| 146 | : ref(1), | 
|---|
| 147 | cStart(0), | 
|---|
| 148 | fillRule(Qt::OddEvenFill), | 
|---|
| 149 | require_moveTo(false), | 
|---|
| 150 | dirtyBounds(false), | 
|---|
| 151 | dirtyControlBounds(false), | 
|---|
| 152 | convex(false), | 
|---|
| 153 | pathConverter(nullptr) | 
|---|
| 154 | { | 
|---|
| 155 | } | 
|---|
| 156 |  | 
|---|
| 157 | QPainterPathPrivate(const QPainterPathPrivate &other) noexcept | 
|---|
| 158 | : ref(1), | 
|---|
| 159 | elements(other.elements), | 
|---|
| 160 | cStart(other.cStart), | 
|---|
| 161 | fillRule(other.fillRule), | 
|---|
| 162 | bounds(other.bounds), | 
|---|
| 163 | controlBounds(other.controlBounds), | 
|---|
| 164 | require_moveTo(false), | 
|---|
| 165 | dirtyBounds(other.dirtyBounds), | 
|---|
| 166 | dirtyControlBounds(other.dirtyControlBounds), | 
|---|
| 167 | convex(other.convex), | 
|---|
| 168 | pathConverter(nullptr) | 
|---|
| 169 | { | 
|---|
| 170 | } | 
|---|
| 171 |  | 
|---|
| 172 | QPainterPathPrivate &operator=(const QPainterPathPrivate &) = delete; | 
|---|
| 173 | ~QPainterPathPrivate() = default; | 
|---|
| 174 |  | 
|---|
| 175 | inline bool isClosed() const; | 
|---|
| 176 | inline void close(); | 
|---|
| 177 | inline void maybeMoveTo(); | 
|---|
| 178 | inline void clear(); | 
|---|
| 179 |  | 
|---|
| 180 | const QVectorPath &vectorPath() { | 
|---|
| 181 | if (!pathConverter) | 
|---|
| 182 | pathConverter.reset(new QVectorPathConverter(elements, fillRule, convex)); | 
|---|
| 183 | return pathConverter->path; | 
|---|
| 184 | } | 
|---|
| 185 |  | 
|---|
| 186 | private: | 
|---|
| 187 | QAtomicInt ref; | 
|---|
| 188 | QList<QPainterPath::Element> elements; | 
|---|
| 189 |  | 
|---|
| 190 | int cStart; | 
|---|
| 191 | Qt::FillRule fillRule; | 
|---|
| 192 |  | 
|---|
| 193 | QRectF bounds; | 
|---|
| 194 | QRectF controlBounds; | 
|---|
| 195 |  | 
|---|
| 196 | uint require_moveTo : 1; | 
|---|
| 197 | uint dirtyBounds : 1; | 
|---|
| 198 | uint dirtyControlBounds : 1; | 
|---|
| 199 | uint convex : 1; | 
|---|
| 200 |  | 
|---|
| 201 | std::unique_ptr<QVectorPathConverter> pathConverter; | 
|---|
| 202 | }; | 
|---|
| 203 |  | 
|---|
| 204 | class QPainterPathStrokerPrivate | 
|---|
| 205 | { | 
|---|
| 206 | public: | 
|---|
| 207 | QPainterPathStrokerPrivate(); | 
|---|
| 208 |  | 
|---|
| 209 | QStroker stroker; | 
|---|
| 210 | QList<qfixed> dashPattern; | 
|---|
| 211 | qreal dashOffset; | 
|---|
| 212 | }; | 
|---|
| 213 |  | 
|---|
| 214 | inline const QPainterPath QVectorPath::convertToPainterPath() const | 
|---|
| 215 | { | 
|---|
| 216 | QPainterPath path; | 
|---|
| 217 | path.ensureData(); | 
|---|
| 218 | QPainterPathPrivate *data = path.d_func(); | 
|---|
| 219 | data->elements.reserve(m_count); | 
|---|
| 220 | int index = 0; | 
|---|
| 221 | data->elements[0].x = m_points[index++]; | 
|---|
| 222 | data->elements[0].y = m_points[index++]; | 
|---|
| 223 |  | 
|---|
| 224 | if (m_elements) { | 
|---|
| 225 | data->elements[0].type = m_elements[0]; | 
|---|
| 226 | for (int i=1; i<m_count; ++i) { | 
|---|
| 227 | QPainterPath::Element element; | 
|---|
| 228 | element.x = m_points[index++]; | 
|---|
| 229 | element.y = m_points[index++]; | 
|---|
| 230 | element.type = m_elements[i]; | 
|---|
| 231 | data->elements << element; | 
|---|
| 232 | } | 
|---|
| 233 | } else { | 
|---|
| 234 | data->elements[0].type = QPainterPath::MoveToElement; | 
|---|
| 235 | for (int i=1; i<m_count; ++i) { | 
|---|
| 236 | QPainterPath::Element element; | 
|---|
| 237 | element.x = m_points[index++]; | 
|---|
| 238 | element.y = m_points[index++]; | 
|---|
| 239 | element.type = QPainterPath::LineToElement; | 
|---|
| 240 | data->elements << element; | 
|---|
| 241 | } | 
|---|
| 242 | } | 
|---|
| 243 |  | 
|---|
| 244 | if (m_hints & OddEvenFill) | 
|---|
| 245 | data->fillRule = Qt::OddEvenFill; | 
|---|
| 246 | else | 
|---|
| 247 | data->fillRule = Qt::WindingFill; | 
|---|
| 248 | return path; | 
|---|
| 249 | } | 
|---|
| 250 |  | 
|---|
| 251 | void Q_GUI_EXPORT qt_find_ellipse_coords(const QRectF &r, qreal angle, qreal length, | 
|---|
| 252 | QPointF* startPoint, QPointF *endPoint); | 
|---|
| 253 |  | 
|---|
| 254 | inline bool QPainterPathPrivate::isClosed() const | 
|---|
| 255 | { | 
|---|
| 256 | const QPainterPath::Element &first = elements.at(cStart); | 
|---|
| 257 | const QPainterPath::Element &last = elements.last(); | 
|---|
| 258 | return first.x == last.x && first.y == last.y; | 
|---|
| 259 | } | 
|---|
| 260 |  | 
|---|
| 261 | inline void QPainterPathPrivate::close() | 
|---|
| 262 | { | 
|---|
| 263 | Q_ASSERT(ref.loadRelaxed() == 1); | 
|---|
| 264 | require_moveTo = true; | 
|---|
| 265 | const QPainterPath::Element &first = elements.at(cStart); | 
|---|
| 266 | QPainterPath::Element &last = elements.last(); | 
|---|
| 267 | if (first.x != last.x || first.y != last.y) { | 
|---|
| 268 | if (qFuzzyCompare(first.x, last.x) && qFuzzyCompare(first.y, last.y)) { | 
|---|
| 269 | last.x = first.x; | 
|---|
| 270 | last.y = first.y; | 
|---|
| 271 | } else { | 
|---|
| 272 | QPainterPath::Element e = { first.x, first.y, QPainterPath::LineToElement }; | 
|---|
| 273 | elements << e; | 
|---|
| 274 | } | 
|---|
| 275 | } | 
|---|
| 276 | } | 
|---|
| 277 |  | 
|---|
| 278 | inline void QPainterPathPrivate::maybeMoveTo() | 
|---|
| 279 | { | 
|---|
| 280 | if (require_moveTo) { | 
|---|
| 281 | QPainterPath::Element e = elements.last(); | 
|---|
| 282 | e.type = QPainterPath::MoveToElement; | 
|---|
| 283 | elements.append(e); | 
|---|
| 284 | require_moveTo = false; | 
|---|
| 285 | } | 
|---|
| 286 | } | 
|---|
| 287 |  | 
|---|
| 288 | inline void QPainterPathPrivate::clear() | 
|---|
| 289 | { | 
|---|
| 290 | Q_ASSERT(ref.loadRelaxed() == 1); | 
|---|
| 291 |  | 
|---|
| 292 | elements.clear(); | 
|---|
| 293 |  | 
|---|
| 294 | cStart = 0; | 
|---|
| 295 | fillRule = Qt::OddEvenFill; | 
|---|
| 296 | bounds = {}; | 
|---|
| 297 | controlBounds = {}; | 
|---|
| 298 |  | 
|---|
| 299 | require_moveTo = false; | 
|---|
| 300 | dirtyBounds = false; | 
|---|
| 301 | dirtyControlBounds = false; | 
|---|
| 302 | convex = false; | 
|---|
| 303 |  | 
|---|
| 304 | pathConverter.reset(); | 
|---|
| 305 | } | 
|---|
| 306 | #define KAPPA qreal(0.5522847498) | 
|---|
| 307 |  | 
|---|
| 308 |  | 
|---|
| 309 | QT_END_NAMESPACE | 
|---|
| 310 |  | 
|---|
| 311 | #endif // QPAINTERPATH_P_H | 
|---|
| 312 |  | 
|---|