| 6 | * License as published by the Free Software Foundation; either |
| 7 | * version 2.1 of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * This library is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | * Lesser General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU Lesser General Public |
| 15 | * License along with this library; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 17 | */ |
| 18 |
| 19 | #ifndef VPATH_H |
| 20 | #define VPATH_H |
| 21 | #include <vector> |
| 22 | #include "vcowptr.h" |
| 23 | #include "vmatrix.h" |
| 24 | #include "vpoint.h" |
| 25 | #include "vrect.h" |
| 26 |
| 27 | V_BEGIN_NAMESPACE |
| 28 |
| 29 | struct VPathData; |
| 30 | class VPath { |
| 31 | public: |
| 32 | enum class Direction { CCW, CW }; |
| 33 |
| 34 | enum class Element : uchar { MoveTo, LineTo, CubicTo, Close }; |
| 35 | bool empty() const; |
| 36 | bool null() const; |
| 37 | void moveTo(const VPointF &p); |
| 38 | void moveTo(float x, float y); |
| 39 | void lineTo(const VPointF &p); |
| 40 | void lineTo(float x, float y); |
| 41 | void cubicTo(const VPointF &c1, const VPointF &c2, const VPointF &e); |
| 42 | void cubicTo(float c1x, float c1y, float c2x, float c2y, float ex, |
| 43 | float ey); |
| 44 | void arcTo(const VRectF &rect, float startAngle, float sweepLength, |
| 45 | bool forceMoveTo); |
| 46 | void close(); |
| 47 | void reset(); |
| 48 | void reserve(size_t pts, size_t elms); |
| 49 | size_t segments() const; |
| 50 | void addCircle(float cx, float cy, float radius, |
| 51 | VPath::Direction dir = Direction::CW); |
| 52 | void addOval(const VRectF &rect, VPath::Direction dir = Direction::CW); |
| 53 | void addRoundRect(const VRectF &rect, float rx, float ry, |
| 54 | VPath::Direction dir = Direction::CW); |
| 55 | void addRoundRect(const VRectF &rect, float roundness, |
| 56 | VPath::Direction dir = Direction::CW); |
| 57 | void addRect(const VRectF &rect, VPath::Direction dir = Direction::CW); |
| 58 | void addPolystar(float points, float innerRadius, float outerRadius, |
| 59 | float innerRoundness, float outerRoundness, |
| 60 | float startAngle, float cx, float cy, |
| 61 | VPath::Direction dir = Direction::CW); |
| 62 | void addPolygon(float points, float radius, float roundness, |
| 63 | float startAngle, float cx, float cy, |
| 64 | VPath::Direction dir = Direction::CW); |
| 65 | void addPath(const VPath &path); |
| 66 | void transform(const VMatrix &m); |
| 67 | float length() const; |
| 68 | const std::vector<VPath::Element> &elements() const; |
| 69 | const std::vector<VPointF> & points() const; |
| 70 | void clone(const VPath &srcPath); |
| 71 | bool unique() const { return d.unique();} |
| 72 | int refCount() const { return d.refCount();} |
| 73 |
| 74 | private: |
| 75 | struct VPathData { |
| 76 | bool empty() const { return m_elements.empty(); } |
| 77 | bool null() const { return empty() && !m_elements.capacity();} |
| 78 | void moveTo(float x, float y); |
| 79 | void lineTo(float x, float y); |
| 80 | void cubicTo(float cx1, float cy1, float cx2, float cy2, float ex, float ey); |
| 81 | void close(); |
| 82 | void reset(); |
| 83 | void reserve(size_t, size_t); |
| 84 | void checkNewSegment(); |
| 85 | size_t segments() const; |
| 86 | void transform(const VMatrix &m); |
| 87 | float length() const; |
| 88 | void addRoundRect(const VRectF &, float, float, VPath::Direction); |
| 89 | void addRoundRect(const VRectF &, float, VPath::Direction); |
| 90 | void addRect(const VRectF &, VPath::Direction); |
| 91 | void arcTo(const VRectF &, float, float, bool); |
| 92 | void addCircle(float, float, float, VPath::Direction); |
| 93 | void addOval(const VRectF &, VPath::Direction); |
| 94 | void addPolystar(float points, float innerRadius, float outerRadius, |
| 95 | float innerRoundness, float outerRoundness, |
| 96 | float startAngle, float cx, float cy, |
| 97 | VPath::Direction dir = Direction::CW); |
| 98 | void addPolygon(float points, float radius, float roundness, |
| 99 | float startAngle, float cx, float cy, |
| 100 | VPath::Direction dir = Direction::CW); |
| 101 | void addPath(const VPathData &path); |
| 102 | void clone(const VPath::VPathData &o) { *this = o;} |
| 103 | const std::vector<VPath::Element> &elements() const |
| 104 | { |
| 105 | return m_elements; |
| 106 | } |
| 107 | const std::vector<VPointF> &points() const { return m_points; } |
| 108 | std::vector<VPointF> m_points; |
| 109 | std::vector<VPath::Element> m_elements; |
| 110 | unsigned int m_segments; |
| 111 | VPointF mStartPoint; |
| 112 | mutable float mLength{0}; |
| 113 | mutable bool mLengthDirty{true}; |
| 114 | bool mNewSegment; |
| 115 | }; |
| 116 |
| 117 | vcow_ptr<VPathData> d; |
| 118 | }; |
| 119 |
| 120 | inline bool VPath::empty() const |
| 121 | { |
| 122 | return d->empty(); |
| 123 | } |
| 124 |
| 125 | /* |
| 126 | * path is empty as well as null(no memory for data allocated yet). |
| 127 | */ |
| 128 | inline bool VPath::null() const |
| 129 | { |
| 130 | return d->null(); |
| 131 | } |
| 132 |
| 133 | inline void VPath::moveTo(const VPointF &p) |
| 134 | { |
| 135 | d.write().moveTo(p.x(), p.y()); |
| 136 | } |
| 137 |
| 138 | inline void VPath::lineTo(const VPointF &p) |
| 139 | { |
| 140 | d.write().lineTo(p.x(), p.y()); |
| 141 | } |
| 142 |
| 143 | inline void VPath::close() |
| 144 | { |
| 145 | d.write().close(); |
| 146 | } |
| 147 |
| 148 | inline void VPath::reset() |
| 149 | { |
| 150 | d.write().reset(); |
| 151 | } |
| 152 |
| 153 | inline void VPath::reserve(size_t pts, size_t elms) |
| 154 | { |
| 155 | d.write().reserve(pts, elms); |
| 156 | } |
| 157 |
| 158 | inline size_t VPath::segments() const |
| 159 | { |
| 160 | return d->segments(); |
| 161 | } |
| 162 |
| 163 | inline float VPath::length() const |
| 164 | { |
| 165 | return d->length(); |
| 166 | } |
| 167 |
| 168 | inline void VPath::cubicTo(const VPointF &c1, const VPointF &c2, |
| 169 | const VPointF &e) |
| 170 | { |
| 171 | d.write().cubicTo(c1.x(), c1.y(), c2.x(), c2.y(), e.x(), e.y()); |
| 172 | } |
| 173 |
| 174 | inline void VPath::lineTo(float x, float y) |
| 175 | { |
| 176 | d.write().lineTo(x, y); |
| 177 | } |
| 178 |
| 179 | inline void VPath::moveTo(float x, float y) |
| 180 | { |
| 181 | d.write().moveTo(x, y); |
| 182 | } |
| 183 |
| 184 | inline void VPath::cubicTo(float c1x, float c1y, float c2x, float c2y, float ex, |
| 185 | float ey) |
| 186 | { |
| 187 | d.write().cubicTo(c1x, c1y, c2x, c2y, ex, ey); |
| 188 | } |
| 189 |
| 190 | inline void VPath::transform(const VMatrix &m) |
| 191 | { |
| 192 | d.write().transform(m); |
| 193 | } |
| 194 |
| 195 | inline void VPath::arcTo(const VRectF &rect, float startAngle, |
| 196 | float sweepLength, bool forceMoveTo) |
| 197 | { |
| 198 | d.write().arcTo(rect, startAngle, sweepLength, forceMoveTo); |
| 199 | } |
| 200 |
| 201 | inline void VPath::addRect(const VRectF &rect, VPath::Direction dir) |
| 202 | { |
| 203 | d.write().addRect(rect, dir); |
| 204 | } |
| 205 |
| 206 | inline void VPath::addRoundRect(const VRectF &rect, float rx, float ry, |
| 207 | VPath::Direction dir) |
| 208 | { |
| 209 | d.write().addRoundRect(rect, rx, ry, dir); |
| 210 | } |
| 211 |
| 212 | inline void VPath::addRoundRect(const VRectF &rect, float roundness, |
| 213 | VPath::Direction dir) |
| 214 | { |
| 215 | d.write().addRoundRect(rect, roundness, dir); |
| 216 | } |
| 217 |
| 218 | inline void VPath::addCircle(float cx, float cy, float radius, |
| 219 | VPath::Direction dir) |
| 220 | { |
| 221 | d.write().addCircle(cx, cy, radius, dir); |
| 222 | } |
| 223 |
| 224 | inline void VPath::addOval(const VRectF &rect, VPath::Direction dir) |
| 225 | { |
| 226 | d.write().addOval(rect, dir); |
| 227 | } |
| 228 |
| 229 | inline void VPath::addPolystar(float points, float innerRadius, |
| 230 | float outerRadius, float innerRoundness, |
| 231 | float outerRoundness, float startAngle, float cx, |
| 232 | float cy, VPath::Direction dir) |
| 233 | { |
| 234 | d.write().addPolystar(points, innerRadius, outerRadius, innerRoundness, |
| 235 | outerRoundness, startAngle, cx, cy, dir); |
| 236 | } |
| 237 |
| 238 | inline void VPath::addPolygon(float points, float radius, float roundness, |
| 239 | float startAngle, float cx, float cy, |
| 240 | VPath::Direction dir) |
| 241 | { |
| 242 | d.write().addPolygon(points, radius, roundness, startAngle, cx, cy, dir); |
| 243 | } |
| 244 |
| 245 | inline void VPath::addPath(const VPath &path) |
| 246 | { |
| 247 | if (path.empty()) return; |
| 248 |
| 249 | if (null()) { |
| 250 | *this = path; |
| 251 | } else { |
| 252 | d.write().addPath(path.d.read()); |
| 253 | } |
| 254 | } |
| 255 |
| 256 | inline const std::vector<VPath::Element> &VPath::elements() const |
| 257 | { |
| 258 | return d->elements(); |
| 259 | } |
| 260 |
| 261 | inline const std::vector<VPointF> &VPath::points() const |
| 262 | { |
| 263 | return d->points(); |
| 264 | } |
| 265 |
| 266 | inline void VPath::clone(const VPath &o) |
| 267 | { |
| 268 | d.write().clone(o.d.read()); |
| 269 | } |
| 270 |
| 271 | V_END_NAMESPACE |
| 272 |
| 273 | #endif // VPATH_H |
| 274 |