| 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 | |
| 4 | #ifndef QVECTORPATH_P_H |
| 5 | #define QVECTORPATH_P_H |
| 6 | |
| 7 | // |
| 8 | // W A R N I N G |
| 9 | // ------------- |
| 10 | // |
| 11 | // This file is not part of the Qt API. It exists purely as an |
| 12 | // implementation detail. This header file may change from version to |
| 13 | // version without notice, or even be removed. |
| 14 | // |
| 15 | // We mean it. |
| 16 | // |
| 17 | |
| 18 | #include <QtGui/private/qtguiglobal_p.h> |
| 19 | #include <QtGui/qpaintengine.h> |
| 20 | |
| 21 | #include <private/qpaintengine_p.h> |
| 22 | #include <private/qstroker_p.h> |
| 23 | #include <private/qpainter_p.h> |
| 24 | |
| 25 | |
| 26 | QT_BEGIN_NAMESPACE |
| 27 | |
| 28 | |
| 29 | class QPaintEngineEx; |
| 30 | |
| 31 | typedef void (*qvectorpath_cache_cleanup)(QPaintEngineEx *engine, void *data); |
| 32 | |
| 33 | struct QRealRect { |
| 34 | qreal x1, y1, x2, y2; |
| 35 | }; |
| 36 | |
| 37 | class Q_GUI_EXPORT QVectorPath |
| 38 | { |
| 39 | public: |
| 40 | enum Hint { |
| 41 | // Shape hints, in 0x000000ff, access using shape() |
| 42 | AreaShapeMask = 0x0001, // shape covers an area |
| 43 | NonConvexShapeMask = 0x0002, // shape is not convex |
| 44 | CurvedShapeMask = 0x0004, // shape contains curves... |
| 45 | LinesShapeMask = 0x0008, |
| 46 | RectangleShapeMask = 0x0010, |
| 47 | ShapeMask = 0x001f, |
| 48 | |
| 49 | // Shape hints merged into basic shapes.. |
| 50 | LinesHint = LinesShapeMask, |
| 51 | RectangleHint = AreaShapeMask | RectangleShapeMask, |
| 52 | EllipseHint = AreaShapeMask | CurvedShapeMask, |
| 53 | ConvexPolygonHint = AreaShapeMask, |
| 54 | PolygonHint = AreaShapeMask | NonConvexShapeMask, |
| 55 | RoundedRectHint = AreaShapeMask | CurvedShapeMask, |
| 56 | ArbitraryShapeHint = AreaShapeMask | NonConvexShapeMask | CurvedShapeMask, |
| 57 | |
| 58 | // Other hints |
| 59 | IsCachedHint = 0x0100, // Set if the cache hint is set |
| 60 | ShouldUseCacheHint = 0x0200, // Set if the path should be cached when possible.. |
| 61 | ControlPointRect = 0x0400, // Set if the control point rect has been calculated... |
| 62 | |
| 63 | // Shape rendering specifiers... |
| 64 | OddEvenFill = 0x1000, |
| 65 | WindingFill = 0x2000, |
| 66 | ImplicitClose = 0x4000, |
| 67 | ExplicitOpen = 0x8000 |
| 68 | }; |
| 69 | |
| 70 | // ### Falcon: introduca a struct XY for points so lars is not so confused... |
| 71 | QVectorPath(const qreal *points, |
| 72 | int count, |
| 73 | const QPainterPath::ElementType *elements = nullptr, |
| 74 | uint hints = ArbitraryShapeHint) |
| 75 | : m_elements(elements), |
| 76 | m_points(points), |
| 77 | m_count(count), |
| 78 | m_hints(hints) |
| 79 | { |
| 80 | } |
| 81 | |
| 82 | ~QVectorPath(); |
| 83 | |
| 84 | QRectF controlPointRect() const; |
| 85 | |
| 86 | inline Hint shape() const { return (Hint) (m_hints & ShapeMask); } |
| 87 | inline bool isConvex() const { return (m_hints & NonConvexShapeMask) == 0; } |
| 88 | inline bool isCurved() const { return m_hints & CurvedShapeMask; } |
| 89 | |
| 90 | inline bool isCacheable() const { return m_hints & ShouldUseCacheHint; } |
| 91 | inline bool hasImplicitClose() const { return m_hints & ImplicitClose; } |
| 92 | inline bool hasExplicitOpen() const { return m_hints & ExplicitOpen; } |
| 93 | inline bool hasWindingFill() const { return m_hints & WindingFill; } |
| 94 | |
| 95 | inline void makeCacheable() const { m_hints |= ShouldUseCacheHint; m_cache = nullptr; } |
| 96 | inline uint hints() const { return m_hints; } |
| 97 | |
| 98 | inline const QPainterPath::ElementType *elements() const { return m_elements; } |
| 99 | inline const qreal *points() const { return m_points; } |
| 100 | inline bool isEmpty() const { return m_points == nullptr; } |
| 101 | |
| 102 | inline int elementCount() const { return m_count; } |
| 103 | inline const QPainterPath convertToPainterPath() const; |
| 104 | |
| 105 | static inline uint polygonFlags(QPaintEngine::PolygonDrawMode mode) |
| 106 | { |
| 107 | switch (mode) { |
| 108 | case QPaintEngine::ConvexMode: return ConvexPolygonHint | ImplicitClose; |
| 109 | case QPaintEngine::OddEvenMode: return PolygonHint | OddEvenFill | ImplicitClose; |
| 110 | case QPaintEngine::WindingMode: return PolygonHint | WindingFill | ImplicitClose; |
| 111 | case QPaintEngine::PolylineMode: return PolygonHint | ExplicitOpen; |
| 112 | default: return 0; |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | struct CacheEntry { |
| 117 | QPaintEngineEx *engine; |
| 118 | void *data; |
| 119 | qvectorpath_cache_cleanup cleanup; |
| 120 | CacheEntry *next; |
| 121 | }; |
| 122 | |
| 123 | CacheEntry *addCacheData(QPaintEngineEx *engine, void *data, qvectorpath_cache_cleanup cleanup) const; |
| 124 | inline CacheEntry *lookupCacheData(QPaintEngineEx *engine) const { |
| 125 | Q_ASSERT(m_hints & ShouldUseCacheHint); |
| 126 | CacheEntry *e = m_cache; |
| 127 | while (e) { |
| 128 | if (e->engine == engine) |
| 129 | return e; |
| 130 | e = e->next; |
| 131 | } |
| 132 | return nullptr; |
| 133 | } |
| 134 | |
| 135 | template <typename T> static inline bool isRect(const T *pts, int elementCount) { |
| 136 | return (elementCount == 5 // 5-point polygon, check for closed rect |
| 137 | && pts[0] == pts[8] && pts[1] == pts[9] // last point == first point |
| 138 | && pts[0] == pts[6] && pts[2] == pts[4] // x values equal |
| 139 | && pts[1] == pts[3] && pts[5] == pts[7] // y values equal... |
| 140 | && pts[0] < pts[4] && pts[1] < pts[5] |
| 141 | ) || |
| 142 | (elementCount == 4 // 4-point polygon, check for unclosed rect |
| 143 | && pts[0] == pts[6] && pts[2] == pts[4] // x values equal |
| 144 | && pts[1] == pts[3] && pts[5] == pts[7] // y values equal... |
| 145 | && pts[0] < pts[4] && pts[1] < pts[5] |
| 146 | ); |
| 147 | } |
| 148 | |
| 149 | inline bool isRect() const |
| 150 | { |
| 151 | const QPainterPath::ElementType * const types = elements(); |
| 152 | |
| 153 | return (shape() == QVectorPath::RectangleHint) |
| 154 | || (isRect(pts: points(), elementCount: elementCount()) |
| 155 | && (!types || (types[0] == QPainterPath::MoveToElement |
| 156 | && types[1] == QPainterPath::LineToElement |
| 157 | && types[2] == QPainterPath::LineToElement |
| 158 | && types[3] == QPainterPath::LineToElement))); |
| 159 | } |
| 160 | |
| 161 | |
| 162 | private: |
| 163 | Q_DISABLE_COPY_MOVE(QVectorPath) |
| 164 | |
| 165 | const QPainterPath::ElementType *m_elements; |
| 166 | const qreal *m_points; |
| 167 | const int m_count; |
| 168 | |
| 169 | mutable uint m_hints; |
| 170 | mutable QRealRect m_cp_rect; |
| 171 | |
| 172 | mutable CacheEntry *m_cache; |
| 173 | }; |
| 174 | |
| 175 | Q_GUI_EXPORT const QVectorPath &qtVectorPathForPath(const QPainterPath &path); |
| 176 | |
| 177 | QT_END_NAMESPACE |
| 178 | |
| 179 | #endif |
| 180 | |