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 | #include "qpainterpath.h" |
41 | #include "qpainterpath_p.h" |
42 | |
43 | #include <qbitmap.h> |
44 | #include <qdebug.h> |
45 | #include <qiodevice.h> |
46 | #include <qlist.h> |
47 | #include <qmatrix.h> |
48 | #include <qpen.h> |
49 | #include <qpolygon.h> |
50 | #include <qtextlayout.h> |
51 | #include <qvarlengtharray.h> |
52 | #include <qmath.h> |
53 | |
54 | #include <private/qbezier_p.h> |
55 | #include <private/qfontengine_p.h> |
56 | #include <private/qnumeric_p.h> |
57 | #include <private/qobject_p.h> |
58 | #include <private/qpathclipper_p.h> |
59 | #include <private/qstroker_p.h> |
60 | #include <private/qtextengine_p.h> |
61 | |
62 | #include <limits.h> |
63 | |
64 | #if 0 |
65 | #include <performance.h> |
66 | #else |
67 | #define PM_INIT |
68 | #define PM_MEASURE(x) |
69 | #define PM_DISPLAY |
70 | #endif |
71 | |
72 | QT_BEGIN_NAMESPACE |
73 | |
74 | static inline bool isValidCoord(qreal c) |
75 | { |
76 | if (sizeof(qreal) >= sizeof(double)) |
77 | return qIsFinite(d: c) && fabs(x: c) < 1e128; |
78 | else |
79 | return qIsFinite(d: c) && fabsf(x: float(c)) < 1e16f; |
80 | } |
81 | |
82 | static bool hasValidCoords(QPointF p) |
83 | { |
84 | return isValidCoord(c: p.x()) && isValidCoord(c: p.y()); |
85 | } |
86 | |
87 | static bool hasValidCoords(QRectF r) |
88 | { |
89 | return isValidCoord(c: r.x()) && isValidCoord(c: r.y()) && isValidCoord(c: r.width()) && isValidCoord(c: r.height()); |
90 | } |
91 | |
92 | struct QPainterPathPrivateDeleter |
93 | { |
94 | static inline void cleanup(QPainterPathPrivate *d) |
95 | { |
96 | // note - we must downcast to QPainterPathData since QPainterPathPrivate |
97 | // has a non-virtual destructor! |
98 | if (d && !d->ref.deref()) |
99 | delete static_cast<QPainterPathData *>(d); |
100 | } |
101 | }; |
102 | |
103 | // This value is used to determine the length of control point vectors |
104 | // when approximating arc segments as curves. The factor is multiplied |
105 | // with the radius of the circle. |
106 | |
107 | // #define QPP_DEBUG |
108 | // #define QPP_STROKE_DEBUG |
109 | //#define QPP_FILLPOLYGONS_DEBUG |
110 | |
111 | QPainterPath qt_stroke_dash(const QPainterPath &path, qreal *dashes, int dashCount); |
112 | |
113 | void qt_find_ellipse_coords(const QRectF &r, qreal angle, qreal length, |
114 | QPointF* startPoint, QPointF *endPoint) |
115 | { |
116 | if (r.isNull()) { |
117 | if (startPoint) |
118 | *startPoint = QPointF(); |
119 | if (endPoint) |
120 | *endPoint = QPointF(); |
121 | return; |
122 | } |
123 | |
124 | qreal w2 = r.width() / 2; |
125 | qreal h2 = r.height() / 2; |
126 | |
127 | qreal angles[2] = { angle, angle + length }; |
128 | QPointF *points[2] = { startPoint, endPoint }; |
129 | |
130 | for (int i = 0; i < 2; ++i) { |
131 | if (!points[i]) |
132 | continue; |
133 | |
134 | qreal theta = angles[i] - 360 * qFloor(v: angles[i] / 360); |
135 | qreal t = theta / 90; |
136 | // truncate |
137 | int quadrant = int(t); |
138 | t -= quadrant; |
139 | |
140 | t = qt_t_for_arc_angle(angle: 90 * t); |
141 | |
142 | // swap x and y? |
143 | if (quadrant & 1) |
144 | t = 1 - t; |
145 | |
146 | qreal a, b, c, d; |
147 | QBezier::coefficients(t, a, b, c, d); |
148 | QPointF p(a + b + c*QT_PATH_KAPPA, d + c + b*QT_PATH_KAPPA); |
149 | |
150 | // left quadrants |
151 | if (quadrant == 1 || quadrant == 2) |
152 | p.rx() = -p.x(); |
153 | |
154 | // top quadrants |
155 | if (quadrant == 0 || quadrant == 1) |
156 | p.ry() = -p.y(); |
157 | |
158 | *points[i] = r.center() + QPointF(w2 * p.x(), h2 * p.y()); |
159 | } |
160 | } |
161 | |
162 | #ifdef QPP_DEBUG |
163 | static void qt_debug_path(const QPainterPath &path) |
164 | { |
165 | const char *names[] = { |
166 | "MoveTo " , |
167 | "LineTo " , |
168 | "CurveTo " , |
169 | "CurveToData" |
170 | }; |
171 | |
172 | printf("\nQPainterPath: elementCount=%d\n" , path.elementCount()); |
173 | for (int i=0; i<path.elementCount(); ++i) { |
174 | const QPainterPath::Element &e = path.elementAt(i); |
175 | Q_ASSERT(e.type >= 0 && e.type <= QPainterPath::CurveToDataElement); |
176 | printf(" - %3d:: %s, (%.2f, %.2f)\n" , i, names[e.type], e.x, e.y); |
177 | } |
178 | } |
179 | #endif |
180 | |
181 | /*! |
182 | \class QPainterPath |
183 | \ingroup painting |
184 | \ingroup shared |
185 | \inmodule QtGui |
186 | |
187 | \brief The QPainterPath class provides a container for painting operations, |
188 | enabling graphical shapes to be constructed and reused. |
189 | |
190 | A painter path is an object composed of a number of graphical |
191 | building blocks, such as rectangles, ellipses, lines, and curves. |
192 | Building blocks can be joined in closed subpaths, for example as a |
193 | rectangle or an ellipse. A closed path has coinciding start and |
194 | end points. Or they can exist independently as unclosed subpaths, |
195 | such as lines and curves. |
196 | |
197 | A QPainterPath object can be used for filling, outlining, and |
198 | clipping. To generate fillable outlines for a given painter path, |
199 | use the QPainterPathStroker class. The main advantage of painter |
200 | paths over normal drawing operations is that complex shapes only |
201 | need to be created once; then they can be drawn many times using |
202 | only calls to the QPainter::drawPath() function. |
203 | |
204 | QPainterPath provides a collection of functions that can be used |
205 | to obtain information about the path and its elements. In addition |
206 | it is possible to reverse the order of the elements using the |
207 | toReversed() function. There are also several functions to convert |
208 | this painter path object into a polygon representation. |
209 | |
210 | \tableofcontents |
211 | |
212 | \section1 Composing a QPainterPath |
213 | |
214 | A QPainterPath object can be constructed as an empty path, with a |
215 | given start point, or as a copy of another QPainterPath object. |
216 | Once created, lines and curves can be added to the path using the |
217 | lineTo(), arcTo(), cubicTo() and quadTo() functions. The lines and |
218 | curves stretch from the currentPosition() to the position passed |
219 | as argument. |
220 | |
221 | The currentPosition() of the QPainterPath object is always the end |
222 | position of the last subpath that was added (or the initial start |
223 | point). Use the moveTo() function to move the currentPosition() |
224 | without adding a component. The moveTo() function implicitly |
225 | starts a new subpath, and closes the previous one. Another way of |
226 | starting a new subpath is to call the closeSubpath() function |
227 | which closes the current path by adding a line from the |
228 | currentPosition() back to the path's start position. Note that the |
229 | new path will have (0, 0) as its initial currentPosition(). |
230 | |
231 | QPainterPath class also provides several convenience functions to |
232 | add closed subpaths to a painter path: addEllipse(), addPath(), |
233 | addRect(), addRegion() and addText(). The addPolygon() function |
234 | adds an \e unclosed subpath. In fact, these functions are all |
235 | collections of moveTo(), lineTo() and cubicTo() operations. |
236 | |
237 | In addition, a path can be added to the current path using the |
238 | connectPath() function. But note that this function will connect |
239 | the last element of the current path to the first element of given |
240 | one by adding a line. |
241 | |
242 | Below is a code snippet that shows how a QPainterPath object can |
243 | be used: |
244 | |
245 | \table 70% |
246 | \row |
247 | \li \inlineimage qpainterpath-construction.png |
248 | \li |
249 | \snippet code/src_gui_painting_qpainterpath.cpp 0 |
250 | \endtable |
251 | |
252 | The painter path is initially empty when constructed. We first add |
253 | a rectangle, which is a closed subpath. Then we add two bezier |
254 | curves which together form a closed subpath even though they are |
255 | not closed individually. Finally we draw the entire path. The path |
256 | is filled using the default fill rule, Qt::OddEvenFill. Qt |
257 | provides two methods for filling paths: |
258 | |
259 | \table |
260 | \header |
261 | \li Qt::OddEvenFill |
262 | \li Qt::WindingFill |
263 | \row |
264 | \li \inlineimage qt-fillrule-oddeven.png |
265 | \li \inlineimage qt-fillrule-winding.png |
266 | \endtable |
267 | |
268 | See the Qt::FillRule documentation for the definition of the |
269 | rules. A painter path's currently set fill rule can be retrieved |
270 | using the fillRule() function, and altered using the setFillRule() |
271 | function. |
272 | |
273 | \section1 QPainterPath Information |
274 | |
275 | The QPainterPath class provides a collection of functions that |
276 | returns information about the path and its elements. |
277 | |
278 | The currentPosition() function returns the end point of the last |
279 | subpath that was added (or the initial start point). The |
280 | elementAt() function can be used to retrieve the various subpath |
281 | elements, the \e number of elements can be retrieved using the |
282 | elementCount() function, and the isEmpty() function tells whether |
283 | this QPainterPath object contains any elements at all. |
284 | |
285 | The controlPointRect() function returns the rectangle containing |
286 | all the points and control points in this path. This function is |
287 | significantly faster to compute than the exact boundingRect() |
288 | which returns the bounding rectangle of this painter path with |
289 | floating point precision. |
290 | |
291 | Finally, QPainterPath provides the contains() function which can |
292 | be used to determine whether a given point or rectangle is inside |
293 | the path, and the intersects() function which determines if any of |
294 | the points inside a given rectangle also are inside this path. |
295 | |
296 | \section1 QPainterPath Conversion |
297 | |
298 | For compatibility reasons, it might be required to simplify the |
299 | representation of a painter path: QPainterPath provides the |
300 | toFillPolygon(), toFillPolygons() and toSubpathPolygons() |
301 | functions which convert the painter path into a polygon. The |
302 | toFillPolygon() returns the painter path as one single polygon, |
303 | while the two latter functions return a list of polygons. |
304 | |
305 | The toFillPolygons() and toSubpathPolygons() functions are |
306 | provided because it is usually faster to draw several small |
307 | polygons than to draw one large polygon, even though the total |
308 | number of points drawn is the same. The difference between the two |
309 | is the \e number of polygons they return: The toSubpathPolygons() |
310 | creates one polygon for each subpath regardless of intersecting |
311 | subpaths (i.e. overlapping bounding rectangles), while the |
312 | toFillPolygons() functions creates only one polygon for |
313 | overlapping subpaths. |
314 | |
315 | The toFillPolygon() and toFillPolygons() functions first convert |
316 | all the subpaths to polygons, then uses a rewinding technique to |
317 | make sure that overlapping subpaths can be filled using the |
318 | correct fill rule. Note that rewinding inserts additional lines in |
319 | the polygon so the outline of the fill polygon does not match the |
320 | outline of the path. |
321 | |
322 | \section1 Examples |
323 | |
324 | Qt provides the \l {painting/painterpaths}{Painter Paths Example} |
325 | and the \l {painting/deform}{Vector Deformation example} which are |
326 | located in Qt's example directory. |
327 | |
328 | The \l {painting/painterpaths}{Painter Paths Example} shows how |
329 | painter paths can be used to build complex shapes for rendering |
330 | and lets the user experiment with the filling and stroking. The |
331 | \l {painting/deform}{Vector Deformation Example} shows how to use |
332 | QPainterPath to draw text. |
333 | |
334 | \table |
335 | \header |
336 | \li \l {painting/painterpaths}{Painter Paths Example} |
337 | \li \l {painting/deform}{Vector Deformation Example} |
338 | \row |
339 | \li \inlineimage qpainterpath-example.png |
340 | \li \inlineimage qpainterpath-demo.png |
341 | \endtable |
342 | |
343 | \sa QPainterPathStroker, QPainter, QRegion, {Painter Paths Example} |
344 | */ |
345 | |
346 | /*! |
347 | \enum QPainterPath::ElementType |
348 | |
349 | This enum describes the types of elements used to connect vertices |
350 | in subpaths. |
351 | |
352 | Note that elements added as closed subpaths using the |
353 | addEllipse(), addPath(), addPolygon(), addRect(), addRegion() and |
354 | addText() convenience functions, is actually added to the path as |
355 | a collection of separate elements using the moveTo(), lineTo() and |
356 | cubicTo() functions. |
357 | |
358 | \value MoveToElement A new subpath. See also moveTo(). |
359 | \value LineToElement A line. See also lineTo(). |
360 | \value CurveToElement A curve. See also cubicTo() and quadTo(). |
361 | \value CurveToDataElement The extra data required to describe a curve in |
362 | a CurveToElement element. |
363 | |
364 | \sa elementAt(), elementCount() |
365 | */ |
366 | |
367 | /*! |
368 | \class QPainterPath::Element |
369 | \inmodule QtGui |
370 | |
371 | \brief The QPainterPath::Element class specifies the position and |
372 | type of a subpath. |
373 | |
374 | Once a QPainterPath object is constructed, subpaths like lines and |
375 | curves can be added to the path (creating |
376 | QPainterPath::LineToElement and QPainterPath::CurveToElement |
377 | components). |
378 | |
379 | The lines and curves stretch from the currentPosition() to the |
380 | position passed as argument. The currentPosition() of the |
381 | QPainterPath object is always the end position of the last subpath |
382 | that was added (or the initial start point). The moveTo() function |
383 | can be used to move the currentPosition() without adding a line or |
384 | curve, creating a QPainterPath::MoveToElement component. |
385 | |
386 | \sa QPainterPath |
387 | */ |
388 | |
389 | /*! |
390 | \variable QPainterPath::Element::x |
391 | \brief the x coordinate of the element's position. |
392 | |
393 | \sa {operator QPointF()} |
394 | */ |
395 | |
396 | /*! |
397 | \variable QPainterPath::Element::y |
398 | \brief the y coordinate of the element's position. |
399 | |
400 | \sa {operator QPointF()} |
401 | */ |
402 | |
403 | /*! |
404 | \variable QPainterPath::Element::type |
405 | \brief the type of element |
406 | |
407 | \sa isCurveTo(), isLineTo(), isMoveTo() |
408 | */ |
409 | |
410 | /*! |
411 | \fn bool QPainterPath::Element::operator==(const Element &other) const |
412 | \since 4.2 |
413 | |
414 | Returns \c true if this element is equal to \a other; |
415 | otherwise returns \c false. |
416 | |
417 | \sa operator!=() |
418 | */ |
419 | |
420 | /*! |
421 | \fn bool QPainterPath::Element::operator!=(const Element &other) const |
422 | \since 4.2 |
423 | |
424 | Returns \c true if this element is not equal to \a other; |
425 | otherwise returns \c false. |
426 | |
427 | \sa operator==() |
428 | */ |
429 | |
430 | /*! |
431 | \fn bool QPainterPath::Element::isCurveTo () const |
432 | |
433 | Returns \c true if the element is a curve, otherwise returns \c false. |
434 | |
435 | \sa type, QPainterPath::CurveToElement |
436 | */ |
437 | |
438 | /*! |
439 | \fn bool QPainterPath::Element::isLineTo () const |
440 | |
441 | Returns \c true if the element is a line, otherwise returns \c false. |
442 | |
443 | \sa type, QPainterPath::LineToElement |
444 | */ |
445 | |
446 | /*! |
447 | \fn bool QPainterPath::Element::isMoveTo () const |
448 | |
449 | Returns \c true if the element is moving the current position, |
450 | otherwise returns \c false. |
451 | |
452 | \sa type, QPainterPath::MoveToElement |
453 | */ |
454 | |
455 | /*! |
456 | \fn QPainterPath::Element::operator QPointF () const |
457 | |
458 | Returns the element's position. |
459 | |
460 | \sa x, y |
461 | */ |
462 | |
463 | /*! |
464 | \fn void QPainterPath::addEllipse(qreal x, qreal y, qreal width, qreal height) |
465 | \overload |
466 | |
467 | Creates an ellipse within the bounding rectangle defined by its top-left |
468 | corner at (\a x, \a y), \a width and \a height, and adds it to the |
469 | painter path as a closed subpath. |
470 | */ |
471 | |
472 | /*! |
473 | \since 4.4 |
474 | |
475 | \fn void QPainterPath::addEllipse(const QPointF ¢er, qreal rx, qreal ry) |
476 | \overload |
477 | |
478 | Creates an ellipse positioned at \a{center} with radii \a{rx} and \a{ry}, |
479 | and adds it to the painter path as a closed subpath. |
480 | */ |
481 | |
482 | /*! |
483 | \fn void QPainterPath::addText(qreal x, qreal y, const QFont &font, const QString &text) |
484 | \overload |
485 | |
486 | Adds the given \a text to this path as a set of closed subpaths created |
487 | from the \a font supplied. The subpaths are positioned so that the left |
488 | end of the text's baseline lies at the point specified by (\a x, \a y). |
489 | */ |
490 | |
491 | /*! |
492 | \fn int QPainterPath::elementCount() const |
493 | |
494 | Returns the number of path elements in the painter path. |
495 | |
496 | \sa ElementType, elementAt(), isEmpty() |
497 | */ |
498 | |
499 | int QPainterPath::elementCount() const |
500 | { |
501 | return d_ptr ? d_ptr->elements.size() : 0; |
502 | } |
503 | |
504 | /*! |
505 | \fn QPainterPath::Element QPainterPath::elementAt(int index) const |
506 | |
507 | Returns the element at the given \a index in the painter path. |
508 | |
509 | \sa ElementType, elementCount(), isEmpty() |
510 | */ |
511 | |
512 | QPainterPath::Element QPainterPath::elementAt(int i) const |
513 | { |
514 | Q_ASSERT(d_ptr); |
515 | Q_ASSERT(i >= 0 && i < elementCount()); |
516 | return d_ptr->elements.at(i); |
517 | } |
518 | |
519 | /*! |
520 | \fn void QPainterPath::setElementPositionAt(int index, qreal x, qreal y) |
521 | \since 4.2 |
522 | |
523 | Sets the x and y coordinate of the element at index \a index to \a |
524 | x and \a y. |
525 | */ |
526 | |
527 | void QPainterPath::setElementPositionAt(int i, qreal x, qreal y) |
528 | { |
529 | Q_ASSERT(d_ptr); |
530 | Q_ASSERT(i >= 0 && i < elementCount()); |
531 | detach(); |
532 | QPainterPath::Element &e = d_ptr->elements[i]; |
533 | e.x = x; |
534 | e.y = y; |
535 | } |
536 | |
537 | |
538 | /*### |
539 | \fn QPainterPath &QPainterPath::operator +=(const QPainterPath &other) |
540 | |
541 | Appends the \a other painter path to this painter path and returns a |
542 | reference to the result. |
543 | */ |
544 | |
545 | /*! |
546 | Constructs an empty QPainterPath object. |
547 | */ |
548 | QPainterPath::QPainterPath() noexcept |
549 | : d_ptr(nullptr) |
550 | { |
551 | } |
552 | |
553 | /*! |
554 | \fn QPainterPath::QPainterPath(const QPainterPath &path) |
555 | |
556 | Creates a QPainterPath object that is a copy of the given \a path. |
557 | |
558 | \sa operator=() |
559 | */ |
560 | QPainterPath::QPainterPath(const QPainterPath &other) |
561 | : d_ptr(other.d_ptr.data()) |
562 | { |
563 | if (d_ptr) |
564 | d_ptr->ref.ref(); |
565 | } |
566 | |
567 | /*! |
568 | Creates a QPainterPath object with the given \a startPoint as its |
569 | current position. |
570 | */ |
571 | |
572 | QPainterPath::QPainterPath(const QPointF &startPoint) |
573 | : d_ptr(new QPainterPathData) |
574 | { |
575 | Element e = { .x: startPoint.x(), .y: startPoint.y(), .type: MoveToElement }; |
576 | d_func()->elements << e; |
577 | } |
578 | |
579 | void QPainterPath::detach() |
580 | { |
581 | if (d_ptr->ref.loadRelaxed() != 1) |
582 | detach_helper(); |
583 | setDirty(true); |
584 | } |
585 | |
586 | /*! |
587 | \internal |
588 | */ |
589 | void QPainterPath::detach_helper() |
590 | { |
591 | QPainterPathPrivate *data = new QPainterPathData(*d_func()); |
592 | d_ptr.reset(other: data); |
593 | } |
594 | |
595 | /*! |
596 | \internal |
597 | */ |
598 | void QPainterPath::ensureData_helper() |
599 | { |
600 | QPainterPathPrivate *data = new QPainterPathData; |
601 | data->elements.reserve(asize: 16); |
602 | QPainterPath::Element e = { .x: 0, .y: 0, .type: QPainterPath::MoveToElement }; |
603 | data->elements << e; |
604 | d_ptr.reset(other: data); |
605 | Q_ASSERT(d_ptr != nullptr); |
606 | } |
607 | |
608 | /*! |
609 | \fn QPainterPath &QPainterPath::operator=(const QPainterPath &path) |
610 | |
611 | Assigns the given \a path to this painter path. |
612 | |
613 | \sa QPainterPath() |
614 | */ |
615 | QPainterPath &QPainterPath::operator=(const QPainterPath &other) |
616 | { |
617 | if (other.d_func() != d_func()) { |
618 | QPainterPathPrivate *data = other.d_func(); |
619 | if (data) |
620 | data->ref.ref(); |
621 | d_ptr.reset(other: data); |
622 | } |
623 | return *this; |
624 | } |
625 | |
626 | /*! |
627 | \fn QPainterPath &QPainterPath::operator=(QPainterPath &&other) |
628 | |
629 | Move-assigns \a other to this QPainterPath instance. |
630 | |
631 | \since 5.2 |
632 | */ |
633 | |
634 | /*! |
635 | \fn void QPainterPath::swap(QPainterPath &other) |
636 | \since 4.8 |
637 | |
638 | Swaps painter path \a other with this painter path. This operation is very |
639 | fast and never fails. |
640 | */ |
641 | |
642 | /*! |
643 | Destroys this QPainterPath object. |
644 | */ |
645 | QPainterPath::~QPainterPath() |
646 | { |
647 | } |
648 | |
649 | /*! |
650 | Clears the path elements stored. |
651 | |
652 | This allows the path to reuse previous memory allocations. |
653 | |
654 | \sa reserve(), capacity() |
655 | \since 5.13 |
656 | */ |
657 | void QPainterPath::clear() |
658 | { |
659 | if (!d_ptr) |
660 | return; |
661 | |
662 | detach(); |
663 | d_func()->clear(); |
664 | d_func()->elements.append( t: {.x: 0, .y: 0, .type: MoveToElement} ); |
665 | } |
666 | |
667 | /*! |
668 | Reserves a given amount of elements in QPainterPath's internal memory. |
669 | |
670 | Attempts to allocate memory for at least \a size elements. |
671 | |
672 | \sa clear(), capacity(), QVector::reserve() |
673 | \since 5.13 |
674 | */ |
675 | void QPainterPath::reserve(int size) |
676 | { |
677 | Q_D(QPainterPath); |
678 | if ((!d && size > 0) || (d && d->elements.capacity() < size)) { |
679 | ensureData(); |
680 | detach(); |
681 | d_func()->elements.reserve(asize: size); |
682 | } |
683 | } |
684 | |
685 | /*! |
686 | Returns the number of elements allocated by the QPainterPath. |
687 | |
688 | \sa clear(), reserve() |
689 | \since 5.13 |
690 | */ |
691 | int QPainterPath::capacity() const |
692 | { |
693 | Q_D(QPainterPath); |
694 | if (d) |
695 | return d->elements.capacity(); |
696 | |
697 | return 0; |
698 | } |
699 | |
700 | /*! |
701 | Closes the current subpath by drawing a line to the beginning of |
702 | the subpath, automatically starting a new path. The current point |
703 | of the new path is (0, 0). |
704 | |
705 | If the subpath does not contain any elements, this function does |
706 | nothing. |
707 | |
708 | \sa moveTo(), {QPainterPath#Composing a QPainterPath}{Composing |
709 | a QPainterPath} |
710 | */ |
711 | void QPainterPath::closeSubpath() |
712 | { |
713 | #ifdef QPP_DEBUG |
714 | printf("QPainterPath::closeSubpath()\n" ); |
715 | #endif |
716 | if (isEmpty()) |
717 | return; |
718 | detach(); |
719 | |
720 | d_func()->close(); |
721 | } |
722 | |
723 | /*! |
724 | \fn void QPainterPath::moveTo(qreal x, qreal y) |
725 | |
726 | \overload |
727 | |
728 | Moves the current position to (\a{x}, \a{y}) and starts a new |
729 | subpath, implicitly closing the previous path. |
730 | */ |
731 | |
732 | /*! |
733 | \fn void QPainterPath::moveTo(const QPointF &point) |
734 | |
735 | Moves the current point to the given \a point, implicitly starting |
736 | a new subpath and closing the previous one. |
737 | |
738 | \sa closeSubpath(), {QPainterPath#Composing a |
739 | QPainterPath}{Composing a QPainterPath} |
740 | */ |
741 | void QPainterPath::moveTo(const QPointF &p) |
742 | { |
743 | #ifdef QPP_DEBUG |
744 | printf("QPainterPath::moveTo() (%.2f,%.2f)\n" , p.x(), p.y()); |
745 | #endif |
746 | |
747 | if (!hasValidCoords(p)) { |
748 | #ifndef QT_NO_DEBUG |
749 | qWarning(msg: "QPainterPath::moveTo: Adding point with invalid coordinates, ignoring call" ); |
750 | #endif |
751 | return; |
752 | } |
753 | |
754 | ensureData(); |
755 | detach(); |
756 | |
757 | QPainterPathData *d = d_func(); |
758 | Q_ASSERT(!d->elements.isEmpty()); |
759 | |
760 | d->require_moveTo = false; |
761 | |
762 | if (d->elements.constLast().type == MoveToElement) { |
763 | d->elements.last().x = p.x(); |
764 | d->elements.last().y = p.y(); |
765 | } else { |
766 | Element elm = { .x: p.x(), .y: p.y(), .type: MoveToElement }; |
767 | d->elements.append(t: elm); |
768 | } |
769 | d->cStart = d->elements.size() - 1; |
770 | } |
771 | |
772 | /*! |
773 | \fn void QPainterPath::lineTo(qreal x, qreal y) |
774 | |
775 | \overload |
776 | |
777 | Draws a line from the current position to the point (\a{x}, |
778 | \a{y}). |
779 | */ |
780 | |
781 | /*! |
782 | \fn void QPainterPath::lineTo(const QPointF &endPoint) |
783 | |
784 | Adds a straight line from the current position to the given \a |
785 | endPoint. After the line is drawn, the current position is updated |
786 | to be at the end point of the line. |
787 | |
788 | \sa addPolygon(), addRect(), {QPainterPath#Composing a |
789 | QPainterPath}{Composing a QPainterPath} |
790 | */ |
791 | void QPainterPath::lineTo(const QPointF &p) |
792 | { |
793 | #ifdef QPP_DEBUG |
794 | printf("QPainterPath::lineTo() (%.2f,%.2f)\n" , p.x(), p.y()); |
795 | #endif |
796 | |
797 | if (!hasValidCoords(p)) { |
798 | #ifndef QT_NO_DEBUG |
799 | qWarning(msg: "QPainterPath::lineTo: Adding point with invalid coordinates, ignoring call" ); |
800 | #endif |
801 | return; |
802 | } |
803 | |
804 | ensureData(); |
805 | detach(); |
806 | |
807 | QPainterPathData *d = d_func(); |
808 | Q_ASSERT(!d->elements.isEmpty()); |
809 | d->maybeMoveTo(); |
810 | if (p == QPointF(d->elements.constLast())) |
811 | return; |
812 | Element elm = { .x: p.x(), .y: p.y(), .type: LineToElement }; |
813 | d->elements.append(t: elm); |
814 | |
815 | d->convex = d->elements.size() == 3 || (d->elements.size() == 4 && d->isClosed()); |
816 | } |
817 | |
818 | /*! |
819 | \fn void QPainterPath::cubicTo(qreal c1X, qreal c1Y, qreal c2X, |
820 | qreal c2Y, qreal endPointX, qreal endPointY); |
821 | |
822 | \overload |
823 | |
824 | Adds a cubic Bezier curve between the current position and the end |
825 | point (\a{endPointX}, \a{endPointY}) with control points specified |
826 | by (\a{c1X}, \a{c1Y}) and (\a{c2X}, \a{c2Y}). |
827 | */ |
828 | |
829 | /*! |
830 | \fn void QPainterPath::cubicTo(const QPointF &c1, const QPointF &c2, const QPointF &endPoint) |
831 | |
832 | Adds a cubic Bezier curve between the current position and the |
833 | given \a endPoint using the control points specified by \a c1, and |
834 | \a c2. |
835 | |
836 | After the curve is added, the current position is updated to be at |
837 | the end point of the curve. |
838 | |
839 | \table 100% |
840 | \row |
841 | \li \inlineimage qpainterpath-cubicto.png |
842 | \li |
843 | \snippet code/src_gui_painting_qpainterpath.cpp 1 |
844 | \endtable |
845 | |
846 | \sa quadTo(), {QPainterPath#Composing a QPainterPath}{Composing |
847 | a QPainterPath} |
848 | */ |
849 | void QPainterPath::cubicTo(const QPointF &c1, const QPointF &c2, const QPointF &e) |
850 | { |
851 | #ifdef QPP_DEBUG |
852 | printf("QPainterPath::cubicTo() (%.2f,%.2f), (%.2f,%.2f), (%.2f,%.2f)\n" , |
853 | c1.x(), c1.y(), c2.x(), c2.y(), e.x(), e.y()); |
854 | #endif |
855 | |
856 | if (!hasValidCoords(p: c1) || !hasValidCoords(p: c2) || !hasValidCoords(p: e)) { |
857 | #ifndef QT_NO_DEBUG |
858 | qWarning(msg: "QPainterPath::cubicTo: Adding point with invalid coordinates, ignoring call" ); |
859 | #endif |
860 | return; |
861 | } |
862 | |
863 | ensureData(); |
864 | detach(); |
865 | |
866 | QPainterPathData *d = d_func(); |
867 | Q_ASSERT(!d->elements.isEmpty()); |
868 | |
869 | |
870 | // Abort on empty curve as a stroker cannot handle this and the |
871 | // curve is irrelevant anyway. |
872 | if (d->elements.constLast() == c1 && c1 == c2 && c2 == e) |
873 | return; |
874 | |
875 | d->maybeMoveTo(); |
876 | |
877 | Element ce1 = { .x: c1.x(), .y: c1.y(), .type: CurveToElement }; |
878 | Element ce2 = { .x: c2.x(), .y: c2.y(), .type: CurveToDataElement }; |
879 | Element ee = { .x: e.x(), .y: e.y(), .type: CurveToDataElement }; |
880 | d->elements << ce1 << ce2 << ee; |
881 | } |
882 | |
883 | /*! |
884 | \fn void QPainterPath::quadTo(qreal cx, qreal cy, qreal endPointX, qreal endPointY); |
885 | |
886 | \overload |
887 | |
888 | Adds a quadratic Bezier curve between the current point and the endpoint |
889 | (\a{endPointX}, \a{endPointY}) with the control point specified by |
890 | (\a{cx}, \a{cy}). |
891 | */ |
892 | |
893 | /*! |
894 | \fn void QPainterPath::quadTo(const QPointF &c, const QPointF &endPoint) |
895 | |
896 | Adds a quadratic Bezier curve between the current position and the |
897 | given \a endPoint with the control point specified by \a c. |
898 | |
899 | After the curve is added, the current point is updated to be at |
900 | the end point of the curve. |
901 | |
902 | \sa cubicTo(), {QPainterPath#Composing a QPainterPath}{Composing a |
903 | QPainterPath} |
904 | */ |
905 | void QPainterPath::quadTo(const QPointF &c, const QPointF &e) |
906 | { |
907 | #ifdef QPP_DEBUG |
908 | printf("QPainterPath::quadTo() (%.2f,%.2f), (%.2f,%.2f)\n" , |
909 | c.x(), c.y(), e.x(), e.y()); |
910 | #endif |
911 | |
912 | if (!hasValidCoords(p: c) || !hasValidCoords(p: e)) { |
913 | #ifndef QT_NO_DEBUG |
914 | qWarning(msg: "QPainterPath::quadTo: Adding point with invalid coordinates, ignoring call" ); |
915 | #endif |
916 | return; |
917 | } |
918 | |
919 | ensureData(); |
920 | detach(); |
921 | |
922 | Q_D(QPainterPath); |
923 | Q_ASSERT(!d->elements.isEmpty()); |
924 | const QPainterPath::Element &elm = d->elements.at(i: elementCount()-1); |
925 | QPointF prev(elm.x, elm.y); |
926 | |
927 | // Abort on empty curve as a stroker cannot handle this and the |
928 | // curve is irrelevant anyway. |
929 | if (prev == c && c == e) |
930 | return; |
931 | |
932 | QPointF c1((prev.x() + 2*c.x()) / 3, (prev.y() + 2*c.y()) / 3); |
933 | QPointF c2((e.x() + 2*c.x()) / 3, (e.y() + 2*c.y()) / 3); |
934 | cubicTo(c1, c2, e); |
935 | } |
936 | |
937 | /*! |
938 | \fn void QPainterPath::arcTo(qreal x, qreal y, qreal width, qreal |
939 | height, qreal startAngle, qreal sweepLength) |
940 | |
941 | \overload |
942 | |
943 | Creates an arc that occupies the rectangle QRectF(\a x, \a y, \a |
944 | width, \a height), beginning at the specified \a startAngle and |
945 | extending \a sweepLength degrees counter-clockwise. |
946 | |
947 | */ |
948 | |
949 | /*! |
950 | \fn void QPainterPath::arcTo(const QRectF &rectangle, qreal startAngle, qreal sweepLength) |
951 | |
952 | Creates an arc that occupies the given \a rectangle, beginning at |
953 | the specified \a startAngle and extending \a sweepLength degrees |
954 | counter-clockwise. |
955 | |
956 | Angles are specified in degrees. Clockwise arcs can be specified |
957 | using negative angles. |
958 | |
959 | Note that this function connects the starting point of the arc to |
960 | the current position if they are not already connected. After the |
961 | arc has been added, the current position is the last point in |
962 | arc. To draw a line back to the first point, use the |
963 | closeSubpath() function. |
964 | |
965 | \table 100% |
966 | \row |
967 | \li \inlineimage qpainterpath-arcto.png |
968 | \li |
969 | \snippet code/src_gui_painting_qpainterpath.cpp 2 |
970 | \endtable |
971 | |
972 | \sa arcMoveTo(), addEllipse(), QPainter::drawArc(), QPainter::drawPie(), |
973 | {QPainterPath#Composing a QPainterPath}{Composing a |
974 | QPainterPath} |
975 | */ |
976 | void QPainterPath::arcTo(const QRectF &rect, qreal startAngle, qreal sweepLength) |
977 | { |
978 | #ifdef QPP_DEBUG |
979 | printf("QPainterPath::arcTo() (%.2f, %.2f, %.2f, %.2f, angle=%.2f, sweep=%.2f\n" , |
980 | rect.x(), rect.y(), rect.width(), rect.height(), startAngle, sweepLength); |
981 | #endif |
982 | |
983 | if (!hasValidCoords(r: rect) || !isValidCoord(c: startAngle) || !isValidCoord(c: sweepLength)) { |
984 | #ifndef QT_NO_DEBUG |
985 | qWarning(msg: "QPainterPath::arcTo: Adding point with invalid coordinates, ignoring call" ); |
986 | #endif |
987 | return; |
988 | } |
989 | |
990 | if (rect.isNull()) |
991 | return; |
992 | |
993 | ensureData(); |
994 | detach(); |
995 | |
996 | int point_count; |
997 | QPointF pts[15]; |
998 | QPointF curve_start = qt_curves_for_arc(rect, startAngle, sweepLength, controlPoints: pts, point_count: &point_count); |
999 | |
1000 | lineTo(p: curve_start); |
1001 | for (int i=0; i<point_count; i+=3) { |
1002 | cubicTo(ctrlPt1x: pts[i].x(), ctrlPt1y: pts[i].y(), |
1003 | ctrlPt2x: pts[i+1].x(), ctrlPt2y: pts[i+1].y(), |
1004 | endPtx: pts[i+2].x(), endPty: pts[i+2].y()); |
1005 | } |
1006 | |
1007 | } |
1008 | |
1009 | |
1010 | /*! |
1011 | \fn void QPainterPath::arcMoveTo(qreal x, qreal y, qreal width, qreal height, qreal angle) |
1012 | \overload |
1013 | \since 4.2 |
1014 | |
1015 | Creates a move to that lies on the arc that occupies the |
1016 | QRectF(\a x, \a y, \a width, \a height) at \a angle. |
1017 | */ |
1018 | |
1019 | |
1020 | /*! |
1021 | \fn void QPainterPath::arcMoveTo(const QRectF &rectangle, qreal angle) |
1022 | \since 4.2 |
1023 | |
1024 | Creates a move to that lies on the arc that occupies the given \a |
1025 | rectangle at \a angle. |
1026 | |
1027 | Angles are specified in degrees. Clockwise arcs can be specified |
1028 | using negative angles. |
1029 | |
1030 | \sa moveTo(), arcTo() |
1031 | */ |
1032 | |
1033 | void QPainterPath::arcMoveTo(const QRectF &rect, qreal angle) |
1034 | { |
1035 | if (rect.isNull()) |
1036 | return; |
1037 | |
1038 | QPointF pt; |
1039 | qt_find_ellipse_coords(r: rect, angle, length: 0, startPoint: &pt, endPoint: nullptr); |
1040 | moveTo(p: pt); |
1041 | } |
1042 | |
1043 | |
1044 | |
1045 | /*! |
1046 | \fn QPointF QPainterPath::currentPosition() const |
1047 | |
1048 | Returns the current position of the path. |
1049 | */ |
1050 | QPointF QPainterPath::currentPosition() const |
1051 | { |
1052 | return !d_ptr || d_func()->elements.isEmpty() |
1053 | ? QPointF() |
1054 | : QPointF(d_func()->elements.constLast().x, d_func()->elements.constLast().y); |
1055 | } |
1056 | |
1057 | |
1058 | /*! |
1059 | \fn void QPainterPath::addRect(qreal x, qreal y, qreal width, qreal height) |
1060 | |
1061 | \overload |
1062 | |
1063 | Adds a rectangle at position (\a{x}, \a{y}), with the given \a |
1064 | width and \a height, as a closed subpath. |
1065 | */ |
1066 | |
1067 | /*! |
1068 | \fn void QPainterPath::addRect(const QRectF &rectangle) |
1069 | |
1070 | Adds the given \a rectangle to this path as a closed subpath. |
1071 | |
1072 | The \a rectangle is added as a clockwise set of lines. The painter |
1073 | path's current position after the \a rectangle has been added is |
1074 | at the top-left corner of the rectangle. |
1075 | |
1076 | \table 100% |
1077 | \row |
1078 | \li \inlineimage qpainterpath-addrectangle.png |
1079 | \li |
1080 | \snippet code/src_gui_painting_qpainterpath.cpp 3 |
1081 | \endtable |
1082 | |
1083 | \sa addRegion(), lineTo(), {QPainterPath#Composing a |
1084 | QPainterPath}{Composing a QPainterPath} |
1085 | */ |
1086 | void QPainterPath::addRect(const QRectF &r) |
1087 | { |
1088 | if (!hasValidCoords(r)) { |
1089 | #ifndef QT_NO_DEBUG |
1090 | qWarning(msg: "QPainterPath::addRect: Adding point with invalid coordinates, ignoring call" ); |
1091 | #endif |
1092 | return; |
1093 | } |
1094 | |
1095 | if (r.isNull()) |
1096 | return; |
1097 | |
1098 | ensureData(); |
1099 | detach(); |
1100 | |
1101 | bool first = d_func()->elements.size() < 2; |
1102 | |
1103 | moveTo(x: r.x(), y: r.y()); |
1104 | |
1105 | Element l1 = { .x: r.x() + r.width(), .y: r.y(), .type: LineToElement }; |
1106 | Element l2 = { .x: r.x() + r.width(), .y: r.y() + r.height(), .type: LineToElement }; |
1107 | Element l3 = { .x: r.x(), .y: r.y() + r.height(), .type: LineToElement }; |
1108 | Element l4 = { .x: r.x(), .y: r.y(), .type: LineToElement }; |
1109 | |
1110 | d_func()->elements << l1 << l2 << l3 << l4; |
1111 | d_func()->require_moveTo = true; |
1112 | d_func()->convex = first; |
1113 | } |
1114 | |
1115 | /*! |
1116 | Adds the given \a polygon to the path as an (unclosed) subpath. |
1117 | |
1118 | Note that the current position after the polygon has been added, |
1119 | is the last point in \a polygon. To draw a line back to the first |
1120 | point, use the closeSubpath() function. |
1121 | |
1122 | \table 100% |
1123 | \row |
1124 | \li \inlineimage qpainterpath-addpolygon.png |
1125 | \li |
1126 | \snippet code/src_gui_painting_qpainterpath.cpp 4 |
1127 | \endtable |
1128 | |
1129 | \sa lineTo(), {QPainterPath#Composing a QPainterPath}{Composing |
1130 | a QPainterPath} |
1131 | */ |
1132 | void QPainterPath::addPolygon(const QPolygonF &polygon) |
1133 | { |
1134 | if (polygon.isEmpty()) |
1135 | return; |
1136 | |
1137 | ensureData(); |
1138 | detach(); |
1139 | |
1140 | moveTo(p: polygon.constFirst()); |
1141 | for (int i=1; i<polygon.size(); ++i) { |
1142 | Element elm = { .x: polygon.at(i).x(), .y: polygon.at(i).y(), .type: LineToElement }; |
1143 | d_func()->elements << elm; |
1144 | } |
1145 | } |
1146 | |
1147 | /*! |
1148 | \fn void QPainterPath::addEllipse(const QRectF &boundingRectangle) |
1149 | |
1150 | Creates an ellipse within the specified \a boundingRectangle |
1151 | and adds it to the painter path as a closed subpath. |
1152 | |
1153 | The ellipse is composed of a clockwise curve, starting and |
1154 | finishing at zero degrees (the 3 o'clock position). |
1155 | |
1156 | \table 100% |
1157 | \row |
1158 | \li \inlineimage qpainterpath-addellipse.png |
1159 | \li |
1160 | \snippet code/src_gui_painting_qpainterpath.cpp 5 |
1161 | \endtable |
1162 | |
1163 | \sa arcTo(), QPainter::drawEllipse(), {QPainterPath#Composing a |
1164 | QPainterPath}{Composing a QPainterPath} |
1165 | */ |
1166 | void QPainterPath::addEllipse(const QRectF &boundingRect) |
1167 | { |
1168 | if (!hasValidCoords(r: boundingRect)) { |
1169 | #ifndef QT_NO_DEBUG |
1170 | qWarning(msg: "QPainterPath::addEllipse: Adding point with invalid coordinates, ignoring call" ); |
1171 | #endif |
1172 | return; |
1173 | } |
1174 | |
1175 | if (boundingRect.isNull()) |
1176 | return; |
1177 | |
1178 | ensureData(); |
1179 | detach(); |
1180 | |
1181 | bool first = d_func()->elements.size() < 2; |
1182 | |
1183 | QPointF pts[12]; |
1184 | int point_count; |
1185 | QPointF start = qt_curves_for_arc(rect: boundingRect, startAngle: 0, sweepLength: -360, controlPoints: pts, point_count: &point_count); |
1186 | |
1187 | moveTo(p: start); |
1188 | cubicTo(c1: pts[0], c2: pts[1], e: pts[2]); // 0 -> 270 |
1189 | cubicTo(c1: pts[3], c2: pts[4], e: pts[5]); // 270 -> 180 |
1190 | cubicTo(c1: pts[6], c2: pts[7], e: pts[8]); // 180 -> 90 |
1191 | cubicTo(c1: pts[9], c2: pts[10], e: pts[11]); // 90 - >0 |
1192 | d_func()->require_moveTo = true; |
1193 | |
1194 | d_func()->convex = first; |
1195 | } |
1196 | |
1197 | /*! |
1198 | \fn void QPainterPath::addText(const QPointF &point, const QFont &font, const QString &text) |
1199 | |
1200 | Adds the given \a text to this path as a set of closed subpaths |
1201 | created from the \a font supplied. The subpaths are positioned so |
1202 | that the left end of the text's baseline lies at the specified \a |
1203 | point. |
1204 | |
1205 | \table 100% |
1206 | \row |
1207 | \li \inlineimage qpainterpath-addtext.png |
1208 | \li |
1209 | \snippet code/src_gui_painting_qpainterpath.cpp 6 |
1210 | \endtable |
1211 | |
1212 | \sa QPainter::drawText(), {QPainterPath#Composing a |
1213 | QPainterPath}{Composing a QPainterPath} |
1214 | */ |
1215 | void QPainterPath::addText(const QPointF &point, const QFont &f, const QString &text) |
1216 | { |
1217 | if (text.isEmpty()) |
1218 | return; |
1219 | |
1220 | ensureData(); |
1221 | detach(); |
1222 | |
1223 | QTextLayout layout(text, f); |
1224 | layout.setCacheEnabled(true); |
1225 | |
1226 | QTextOption opt = layout.textOption(); |
1227 | opt.setUseDesignMetrics(true); |
1228 | layout.setTextOption(opt); |
1229 | |
1230 | QTextEngine *eng = layout.engine(); |
1231 | layout.beginLayout(); |
1232 | QTextLine line = layout.createLine(); |
1233 | Q_UNUSED(line); |
1234 | layout.endLayout(); |
1235 | const QScriptLine &sl = eng->lines[0]; |
1236 | if (!sl.length || !eng->layoutData) |
1237 | return; |
1238 | |
1239 | int nItems = eng->layoutData->items.size(); |
1240 | |
1241 | qreal x(point.x()); |
1242 | qreal y(point.y()); |
1243 | |
1244 | QVarLengthArray<int> visualOrder(nItems); |
1245 | QVarLengthArray<uchar> levels(nItems); |
1246 | for (int i = 0; i < nItems; ++i) |
1247 | levels[i] = eng->layoutData->items.at(i).analysis.bidiLevel; |
1248 | QTextEngine::bidiReorder(numRuns: nItems, levels: levels.data(), visualOrder: visualOrder.data()); |
1249 | |
1250 | for (int i = 0; i < nItems; ++i) { |
1251 | int item = visualOrder[i]; |
1252 | const QScriptItem &si = eng->layoutData->items.at(i: item); |
1253 | |
1254 | if (si.analysis.flags < QScriptAnalysis::TabOrObject) { |
1255 | QGlyphLayout glyphs = eng->shapedGlyphs(si: &si); |
1256 | QFontEngine *fe = f.d->engineForScript(script: si.analysis.script); |
1257 | Q_ASSERT(fe); |
1258 | fe->addOutlineToPath(x, y, glyphs, this, |
1259 | flags: si.analysis.bidiLevel % 2 |
1260 | ? QTextItem::RenderFlags(QTextItem::RightToLeft) |
1261 | : QTextItem::RenderFlags{}); |
1262 | |
1263 | const qreal lw = fe->lineThickness().toReal(); |
1264 | if (f.d->underline) { |
1265 | qreal pos = fe->underlinePosition().toReal(); |
1266 | addRect(x, y: y + pos, w: si.width.toReal(), h: lw); |
1267 | } |
1268 | if (f.d->overline) { |
1269 | qreal pos = fe->ascent().toReal() + 1; |
1270 | addRect(x, y: y - pos, w: si.width.toReal(), h: lw); |
1271 | } |
1272 | if (f.d->strikeOut) { |
1273 | qreal pos = fe->ascent().toReal() / 3; |
1274 | addRect(x, y: y - pos, w: si.width.toReal(), h: lw); |
1275 | } |
1276 | } |
1277 | x += si.width.toReal(); |
1278 | } |
1279 | } |
1280 | |
1281 | /*! |
1282 | \fn void QPainterPath::addPath(const QPainterPath &path) |
1283 | |
1284 | Adds the given \a path to \e this path as a closed subpath. |
1285 | |
1286 | \sa connectPath(), {QPainterPath#Composing a |
1287 | QPainterPath}{Composing a QPainterPath} |
1288 | */ |
1289 | void QPainterPath::addPath(const QPainterPath &other) |
1290 | { |
1291 | if (other.isEmpty()) |
1292 | return; |
1293 | |
1294 | ensureData(); |
1295 | detach(); |
1296 | |
1297 | QPainterPathData *d = reinterpret_cast<QPainterPathData *>(d_func()); |
1298 | // Remove last moveto so we don't get multiple moveto's |
1299 | if (d->elements.constLast().type == MoveToElement) |
1300 | d->elements.remove(i: d->elements.size()-1); |
1301 | |
1302 | // Locate where our own current subpath will start after the other path is added. |
1303 | int cStart = d->elements.size() + other.d_func()->cStart; |
1304 | d->elements += other.d_func()->elements; |
1305 | d->cStart = cStart; |
1306 | |
1307 | d->require_moveTo = other.d_func()->isClosed(); |
1308 | } |
1309 | |
1310 | |
1311 | /*! |
1312 | \fn void QPainterPath::connectPath(const QPainterPath &path) |
1313 | |
1314 | Connects the given \a path to \e this path by adding a line from the |
1315 | last element of this path to the first element of the given path. |
1316 | |
1317 | \sa addPath(), {QPainterPath#Composing a QPainterPath}{Composing |
1318 | a QPainterPath} |
1319 | */ |
1320 | void QPainterPath::connectPath(const QPainterPath &other) |
1321 | { |
1322 | if (other.isEmpty()) |
1323 | return; |
1324 | |
1325 | ensureData(); |
1326 | detach(); |
1327 | |
1328 | QPainterPathData *d = reinterpret_cast<QPainterPathData *>(d_func()); |
1329 | // Remove last moveto so we don't get multiple moveto's |
1330 | if (d->elements.constLast().type == MoveToElement) |
1331 | d->elements.remove(i: d->elements.size()-1); |
1332 | |
1333 | // Locate where our own current subpath will start after the other path is added. |
1334 | int cStart = d->elements.size() + other.d_func()->cStart; |
1335 | int first = d->elements.size(); |
1336 | d->elements += other.d_func()->elements; |
1337 | |
1338 | if (first != 0) |
1339 | d->elements[first].type = LineToElement; |
1340 | |
1341 | // avoid duplicate points |
1342 | if (first > 0 && QPointF(d->elements.at(i: first)) == QPointF(d->elements.at(i: first - 1))) { |
1343 | d->elements.remove(i: first--); |
1344 | --cStart; |
1345 | } |
1346 | |
1347 | if (cStart != first) |
1348 | d->cStart = cStart; |
1349 | } |
1350 | |
1351 | /*! |
1352 | Adds the given \a region to the path by adding each rectangle in |
1353 | the region as a separate closed subpath. |
1354 | |
1355 | \sa addRect(), {QPainterPath#Composing a QPainterPath}{Composing |
1356 | a QPainterPath} |
1357 | */ |
1358 | void QPainterPath::addRegion(const QRegion ®ion) |
1359 | { |
1360 | ensureData(); |
1361 | detach(); |
1362 | |
1363 | for (const QRect &rect : region) |
1364 | addRect(r: rect); |
1365 | } |
1366 | |
1367 | |
1368 | /*! |
1369 | Returns the painter path's currently set fill rule. |
1370 | |
1371 | \sa setFillRule() |
1372 | */ |
1373 | Qt::FillRule QPainterPath::fillRule() const |
1374 | { |
1375 | return !d_func() ? Qt::OddEvenFill : d_func()->fillRule; |
1376 | } |
1377 | |
1378 | /*! |
1379 | \fn void QPainterPath::setFillRule(Qt::FillRule fillRule) |
1380 | |
1381 | Sets the fill rule of the painter path to the given \a |
1382 | fillRule. Qt provides two methods for filling paths: |
1383 | |
1384 | \table |
1385 | \header |
1386 | \li Qt::OddEvenFill (default) |
1387 | \li Qt::WindingFill |
1388 | \row |
1389 | \li \inlineimage qt-fillrule-oddeven.png |
1390 | \li \inlineimage qt-fillrule-winding.png |
1391 | \endtable |
1392 | |
1393 | \sa fillRule() |
1394 | */ |
1395 | void QPainterPath::setFillRule(Qt::FillRule fillRule) |
1396 | { |
1397 | ensureData(); |
1398 | if (d_func()->fillRule == fillRule) |
1399 | return; |
1400 | detach(); |
1401 | |
1402 | d_func()->fillRule = fillRule; |
1403 | } |
1404 | |
1405 | #define QT_BEZIER_A(bezier, coord) 3 * (-bezier.coord##1 \ |
1406 | + 3*bezier.coord##2 \ |
1407 | - 3*bezier.coord##3 \ |
1408 | +bezier.coord##4) |
1409 | |
1410 | #define QT_BEZIER_B(bezier, coord) 6 * (bezier.coord##1 \ |
1411 | - 2*bezier.coord##2 \ |
1412 | + bezier.coord##3) |
1413 | |
1414 | #define QT_BEZIER_C(bezier, coord) 3 * (- bezier.coord##1 \ |
1415 | + bezier.coord##2) |
1416 | |
1417 | #define QT_BEZIER_CHECK_T(bezier, t) \ |
1418 | if (t >= 0 && t <= 1) { \ |
1419 | QPointF p(b.pointAt(t)); \ |
1420 | if (p.x() < minx) minx = p.x(); \ |
1421 | else if (p.x() > maxx) maxx = p.x(); \ |
1422 | if (p.y() < miny) miny = p.y(); \ |
1423 | else if (p.y() > maxy) maxy = p.y(); \ |
1424 | } |
1425 | |
1426 | |
1427 | static QRectF (const QBezier &b) |
1428 | { |
1429 | qreal minx, miny, maxx, maxy; |
1430 | |
1431 | // initialize with end points |
1432 | if (b.x1 < b.x4) { |
1433 | minx = b.x1; |
1434 | maxx = b.x4; |
1435 | } else { |
1436 | minx = b.x4; |
1437 | maxx = b.x1; |
1438 | } |
1439 | if (b.y1 < b.y4) { |
1440 | miny = b.y1; |
1441 | maxy = b.y4; |
1442 | } else { |
1443 | miny = b.y4; |
1444 | maxy = b.y1; |
1445 | } |
1446 | |
1447 | // Update for the X extrema |
1448 | { |
1449 | qreal ax = QT_BEZIER_A(b, x); |
1450 | qreal bx = QT_BEZIER_B(b, x); |
1451 | qreal cx = QT_BEZIER_C(b, x); |
1452 | // specialcase quadratic curves to avoid div by zero |
1453 | if (qFuzzyIsNull(d: ax)) { |
1454 | |
1455 | // linear curves are covered by initialization. |
1456 | if (!qFuzzyIsNull(d: bx)) { |
1457 | qreal t = -cx / bx; |
1458 | QT_BEZIER_CHECK_T(b, t); |
1459 | } |
1460 | |
1461 | } else { |
1462 | const qreal tx = bx * bx - 4 * ax * cx; |
1463 | |
1464 | if (tx >= 0) { |
1465 | qreal temp = qSqrt(v: tx); |
1466 | qreal rcp = 1 / (2 * ax); |
1467 | qreal t1 = (-bx + temp) * rcp; |
1468 | QT_BEZIER_CHECK_T(b, t1); |
1469 | |
1470 | qreal t2 = (-bx - temp) * rcp; |
1471 | QT_BEZIER_CHECK_T(b, t2); |
1472 | } |
1473 | } |
1474 | } |
1475 | |
1476 | // Update for the Y extrema |
1477 | { |
1478 | qreal ay = QT_BEZIER_A(b, y); |
1479 | qreal by = QT_BEZIER_B(b, y); |
1480 | qreal cy = QT_BEZIER_C(b, y); |
1481 | |
1482 | // specialcase quadratic curves to avoid div by zero |
1483 | if (qFuzzyIsNull(d: ay)) { |
1484 | |
1485 | // linear curves are covered by initialization. |
1486 | if (!qFuzzyIsNull(d: by)) { |
1487 | qreal t = -cy / by; |
1488 | QT_BEZIER_CHECK_T(b, t); |
1489 | } |
1490 | |
1491 | } else { |
1492 | const qreal ty = by * by - 4 * ay * cy; |
1493 | |
1494 | if (ty > 0) { |
1495 | qreal temp = qSqrt(v: ty); |
1496 | qreal rcp = 1 / (2 * ay); |
1497 | qreal t1 = (-by + temp) * rcp; |
1498 | QT_BEZIER_CHECK_T(b, t1); |
1499 | |
1500 | qreal t2 = (-by - temp) * rcp; |
1501 | QT_BEZIER_CHECK_T(b, t2); |
1502 | } |
1503 | } |
1504 | } |
1505 | return QRectF(minx, miny, maxx - minx, maxy - miny); |
1506 | } |
1507 | |
1508 | /*! |
1509 | Returns the bounding rectangle of this painter path as a rectangle with |
1510 | floating point precision. |
1511 | |
1512 | \sa controlPointRect() |
1513 | */ |
1514 | QRectF QPainterPath::boundingRect() const |
1515 | { |
1516 | if (!d_ptr) |
1517 | return QRectF(); |
1518 | QPainterPathData *d = d_func(); |
1519 | |
1520 | if (d->dirtyBounds) |
1521 | computeBoundingRect(); |
1522 | return d->bounds; |
1523 | } |
1524 | |
1525 | /*! |
1526 | Returns the rectangle containing all the points and control points |
1527 | in this path. |
1528 | |
1529 | This function is significantly faster to compute than the exact |
1530 | boundingRect(), and the returned rectangle is always a superset of |
1531 | the rectangle returned by boundingRect(). |
1532 | |
1533 | \sa boundingRect() |
1534 | */ |
1535 | QRectF QPainterPath::controlPointRect() const |
1536 | { |
1537 | if (!d_ptr) |
1538 | return QRectF(); |
1539 | QPainterPathData *d = d_func(); |
1540 | |
1541 | if (d->dirtyControlBounds) |
1542 | computeControlPointRect(); |
1543 | return d->controlBounds; |
1544 | } |
1545 | |
1546 | |
1547 | /*! |
1548 | \fn bool QPainterPath::isEmpty() const |
1549 | |
1550 | Returns \c true if either there are no elements in this path, or if the only |
1551 | element is a MoveToElement; otherwise returns \c false. |
1552 | |
1553 | \sa elementCount() |
1554 | */ |
1555 | |
1556 | bool QPainterPath::isEmpty() const |
1557 | { |
1558 | return !d_ptr || (d_ptr->elements.size() == 1 && d_ptr->elements.first().type == MoveToElement); |
1559 | } |
1560 | |
1561 | /*! |
1562 | Creates and returns a reversed copy of the path. |
1563 | |
1564 | It is the order of the elements that is reversed: If a |
1565 | QPainterPath is composed by calling the moveTo(), lineTo() and |
1566 | cubicTo() functions in the specified order, the reversed copy is |
1567 | composed by calling cubicTo(), lineTo() and moveTo(). |
1568 | */ |
1569 | QPainterPath QPainterPath::toReversed() const |
1570 | { |
1571 | Q_D(const QPainterPath); |
1572 | QPainterPath rev; |
1573 | |
1574 | if (isEmpty()) { |
1575 | rev = *this; |
1576 | return rev; |
1577 | } |
1578 | |
1579 | rev.moveTo(x: d->elements.at(i: d->elements.size()-1).x, y: d->elements.at(i: d->elements.size()-1).y); |
1580 | |
1581 | for (int i=d->elements.size()-1; i>=1; --i) { |
1582 | const QPainterPath::Element &elm = d->elements.at(i); |
1583 | const QPainterPath::Element &prev = d->elements.at(i: i-1); |
1584 | switch (elm.type) { |
1585 | case LineToElement: |
1586 | rev.lineTo(x: prev.x, y: prev.y); |
1587 | break; |
1588 | case MoveToElement: |
1589 | rev.moveTo(x: prev.x, y: prev.y); |
1590 | break; |
1591 | case CurveToDataElement: |
1592 | { |
1593 | Q_ASSERT(i>=3); |
1594 | const QPainterPath::Element &cp1 = d->elements.at(i: i-2); |
1595 | const QPainterPath::Element &sp = d->elements.at(i: i-3); |
1596 | Q_ASSERT(prev.type == CurveToDataElement); |
1597 | Q_ASSERT(cp1.type == CurveToElement); |
1598 | rev.cubicTo(ctrlPt1x: prev.x, ctrlPt1y: prev.y, ctrlPt2x: cp1.x, ctrlPt2y: cp1.y, endPtx: sp.x, endPty: sp.y); |
1599 | i -= 2; |
1600 | break; |
1601 | } |
1602 | default: |
1603 | Q_ASSERT(!"qt_reversed_path" ); |
1604 | break; |
1605 | } |
1606 | } |
1607 | //qt_debug_path(rev); |
1608 | return rev; |
1609 | } |
1610 | |
1611 | /*! |
1612 | Converts the path into a list of polygons using the QTransform |
1613 | \a matrix, and returns the list. |
1614 | |
1615 | This function creates one polygon for each subpath regardless of |
1616 | intersecting subpaths (i.e. overlapping bounding rectangles). To |
1617 | make sure that such overlapping subpaths are filled correctly, use |
1618 | the toFillPolygons() function instead. |
1619 | |
1620 | \sa toFillPolygons(), toFillPolygon(), {QPainterPath#QPainterPath |
1621 | Conversion}{QPainterPath Conversion} |
1622 | */ |
1623 | QList<QPolygonF> QPainterPath::toSubpathPolygons(const QTransform &matrix) const |
1624 | { |
1625 | |
1626 | Q_D(const QPainterPath); |
1627 | QList<QPolygonF> flatCurves; |
1628 | if (isEmpty()) |
1629 | return flatCurves; |
1630 | |
1631 | QPolygonF current; |
1632 | for (int i=0; i<elementCount(); ++i) { |
1633 | const QPainterPath::Element &e = d->elements.at(i); |
1634 | switch (e.type) { |
1635 | case QPainterPath::MoveToElement: |
1636 | if (current.size() > 1) |
1637 | flatCurves += current; |
1638 | current.clear(); |
1639 | current.reserve(asize: 16); |
1640 | current += QPointF(e.x, e.y) * matrix; |
1641 | break; |
1642 | case QPainterPath::LineToElement: |
1643 | current += QPointF(e.x, e.y) * matrix; |
1644 | break; |
1645 | case QPainterPath::CurveToElement: { |
1646 | Q_ASSERT(d->elements.at(i+1).type == QPainterPath::CurveToDataElement); |
1647 | Q_ASSERT(d->elements.at(i+2).type == QPainterPath::CurveToDataElement); |
1648 | QBezier bezier = QBezier::fromPoints(p1: QPointF(d->elements.at(i: i-1).x, d->elements.at(i: i-1).y) * matrix, |
1649 | p2: QPointF(e.x, e.y) * matrix, |
1650 | p3: QPointF(d->elements.at(i: i+1).x, d->elements.at(i: i+1).y) * matrix, |
1651 | p4: QPointF(d->elements.at(i: i+2).x, d->elements.at(i: i+2).y) * matrix); |
1652 | bezier.addToPolygon(p: ¤t); |
1653 | i+=2; |
1654 | break; |
1655 | } |
1656 | case QPainterPath::CurveToDataElement: |
1657 | Q_ASSERT(!"QPainterPath::toSubpathPolygons(), bad element type" ); |
1658 | break; |
1659 | } |
1660 | } |
1661 | |
1662 | if (current.size()>1) |
1663 | flatCurves += current; |
1664 | |
1665 | return flatCurves; |
1666 | } |
1667 | |
1668 | #if QT_DEPRECATED_SINCE(5, 15) |
1669 | /*! |
1670 | \overload |
1671 | \obsolete |
1672 | |
1673 | Use toSubpathPolygons(const QTransform &matrix) instead. |
1674 | */ |
1675 | QList<QPolygonF> QPainterPath::toSubpathPolygons(const QMatrix &matrix) const |
1676 | { |
1677 | return toSubpathPolygons(matrix: QTransform(matrix)); |
1678 | } |
1679 | #endif // QT_DEPRECATED_SINCE(5, 15) |
1680 | |
1681 | /*! |
1682 | Converts the path into a list of polygons using the |
1683 | QTransform \a matrix, and returns the list. |
1684 | |
1685 | The function differs from the toFillPolygon() function in that it |
1686 | creates several polygons. It is provided because it is usually |
1687 | faster to draw several small polygons than to draw one large |
1688 | polygon, even though the total number of points drawn is the same. |
1689 | |
1690 | The toFillPolygons() function differs from the toSubpathPolygons() |
1691 | function in that it create only polygon for subpaths that have |
1692 | overlapping bounding rectangles. |
1693 | |
1694 | Like the toFillPolygon() function, this function uses a rewinding |
1695 | technique to make sure that overlapping subpaths can be filled |
1696 | using the correct fill rule. Note that rewinding inserts addition |
1697 | lines in the polygons so the outline of the fill polygon does not |
1698 | match the outline of the path. |
1699 | |
1700 | \sa toSubpathPolygons(), toFillPolygon(), |
1701 | {QPainterPath#QPainterPath Conversion}{QPainterPath Conversion} |
1702 | */ |
1703 | QList<QPolygonF> QPainterPath::toFillPolygons(const QTransform &matrix) const |
1704 | { |
1705 | |
1706 | QList<QPolygonF> polys; |
1707 | |
1708 | QList<QPolygonF> subpaths = toSubpathPolygons(matrix); |
1709 | int count = subpaths.size(); |
1710 | |
1711 | if (count == 0) |
1712 | return polys; |
1713 | |
1714 | QVector<QRectF> bounds; |
1715 | bounds.reserve(asize: count); |
1716 | for (int i=0; i<count; ++i) |
1717 | bounds += subpaths.at(i).boundingRect(); |
1718 | |
1719 | #ifdef QPP_FILLPOLYGONS_DEBUG |
1720 | printf("QPainterPath::toFillPolygons, subpathCount=%d\n" , count); |
1721 | for (int i=0; i<bounds.size(); ++i) |
1722 | qDebug() << " bounds" << i << bounds.at(i); |
1723 | #endif |
1724 | |
1725 | QVector< QVector<int> > isects; |
1726 | isects.resize(asize: count); |
1727 | |
1728 | // find all intersections |
1729 | for (int j=0; j<count; ++j) { |
1730 | if (subpaths.at(i: j).size() <= 2) |
1731 | continue; |
1732 | QRectF cbounds = bounds.at(i: j); |
1733 | for (int i=0; i<count; ++i) { |
1734 | if (cbounds.intersects(r: bounds.at(i))) { |
1735 | isects[j] << i; |
1736 | } |
1737 | } |
1738 | } |
1739 | |
1740 | #ifdef QPP_FILLPOLYGONS_DEBUG |
1741 | printf("Intersections before flattening:\n" ); |
1742 | for (int i = 0; i < count; ++i) { |
1743 | printf("%d: " , i); |
1744 | for (int j = 0; j < isects[i].size(); ++j) { |
1745 | printf("%d " , isects[i][j]); |
1746 | } |
1747 | printf("\n" ); |
1748 | } |
1749 | #endif |
1750 | |
1751 | // flatten the sets of intersections |
1752 | for (int i=0; i<count; ++i) { |
1753 | const QVector<int> ¤t_isects = isects.at(i); |
1754 | for (int j=0; j<current_isects.size(); ++j) { |
1755 | int isect_j = current_isects.at(i: j); |
1756 | if (isect_j == i) |
1757 | continue; |
1758 | const QVector<int> &isects_j = isects.at(i: isect_j); |
1759 | for (int k = 0, size = isects_j.size(); k < size; ++k) { |
1760 | int isect_k = isects_j.at(i: k); |
1761 | if (isect_k != i && !isects.at(i).contains(t: isect_k)) { |
1762 | isects[i] += isect_k; |
1763 | } |
1764 | } |
1765 | isects[isect_j].clear(); |
1766 | } |
1767 | } |
1768 | |
1769 | #ifdef QPP_FILLPOLYGONS_DEBUG |
1770 | printf("Intersections after flattening:\n" ); |
1771 | for (int i = 0; i < count; ++i) { |
1772 | printf("%d: " , i); |
1773 | for (int j = 0; j < isects[i].size(); ++j) { |
1774 | printf("%d " , isects[i][j]); |
1775 | } |
1776 | printf("\n" ); |
1777 | } |
1778 | #endif |
1779 | |
1780 | // Join the intersected subpaths as rewinded polygons |
1781 | for (int i=0; i<count; ++i) { |
1782 | const QVector<int> &subpath_list = isects.at(i); |
1783 | if (!subpath_list.isEmpty()) { |
1784 | QPolygonF buildUp; |
1785 | for (int j=0; j<subpath_list.size(); ++j) { |
1786 | const QPolygonF &subpath = subpaths.at(i: subpath_list.at(i: j)); |
1787 | buildUp += subpath; |
1788 | if (!subpath.isClosed()) |
1789 | buildUp += subpath.first(); |
1790 | if (!buildUp.isClosed()) |
1791 | buildUp += buildUp.constFirst(); |
1792 | } |
1793 | polys += buildUp; |
1794 | } |
1795 | } |
1796 | |
1797 | return polys; |
1798 | } |
1799 | |
1800 | #if QT_DEPRECATED_SINCE(5, 15) |
1801 | /*! |
1802 | \overload |
1803 | \obsolete |
1804 | |
1805 | Use toFillPolygons(const QTransform &matrix) instead. |
1806 | */ |
1807 | QList<QPolygonF> QPainterPath::toFillPolygons(const QMatrix &matrix) const |
1808 | { |
1809 | return toFillPolygons(matrix: QTransform(matrix)); |
1810 | } |
1811 | #endif // QT_DEPRECATED_SINCE(5, 15) |
1812 | |
1813 | //same as qt_polygon_isect_line in qpolygon.cpp |
1814 | static void qt_painterpath_isect_line(const QPointF &p1, |
1815 | const QPointF &p2, |
1816 | const QPointF &pos, |
1817 | int *winding) |
1818 | { |
1819 | qreal x1 = p1.x(); |
1820 | qreal y1 = p1.y(); |
1821 | qreal x2 = p2.x(); |
1822 | qreal y2 = p2.y(); |
1823 | qreal y = pos.y(); |
1824 | |
1825 | int dir = 1; |
1826 | |
1827 | if (qFuzzyCompare(p1: y1, p2: y2)) { |
1828 | // ignore horizontal lines according to scan conversion rule |
1829 | return; |
1830 | } else if (y2 < y1) { |
1831 | qreal x_tmp = x2; x2 = x1; x1 = x_tmp; |
1832 | qreal y_tmp = y2; y2 = y1; y1 = y_tmp; |
1833 | dir = -1; |
1834 | } |
1835 | |
1836 | if (y >= y1 && y < y2) { |
1837 | qreal x = x1 + ((x2 - x1) / (y2 - y1)) * (y - y1); |
1838 | |
1839 | // count up the winding number if we're |
1840 | if (x<=pos.x()) { |
1841 | (*winding) += dir; |
1842 | } |
1843 | } |
1844 | } |
1845 | |
1846 | static void qt_painterpath_isect_curve(const QBezier &bezier, const QPointF &pt, |
1847 | int *winding, int depth = 0) |
1848 | { |
1849 | qreal y = pt.y(); |
1850 | qreal x = pt.x(); |
1851 | QRectF bounds = bezier.bounds(); |
1852 | |
1853 | // potential intersection, divide and try again... |
1854 | // Please note that a sideeffect of the bottom exclusion is that |
1855 | // horizontal lines are dropped, but this is correct according to |
1856 | // scan conversion rules. |
1857 | if (y >= bounds.y() && y < bounds.y() + bounds.height()) { |
1858 | |
1859 | // hit lower limit... This is a rough threshold, but its a |
1860 | // tradeoff between speed and precision. |
1861 | const qreal lower_bound = qreal(.001); |
1862 | if (depth == 32 || (bounds.width() < lower_bound && bounds.height() < lower_bound)) { |
1863 | // We make the assumption here that the curve starts to |
1864 | // approximate a line after while (i.e. that it doesn't |
1865 | // change direction drastically during its slope) |
1866 | if (bezier.pt1().x() <= x) { |
1867 | (*winding) += (bezier.pt4().y() > bezier.pt1().y() ? 1 : -1); |
1868 | } |
1869 | return; |
1870 | } |
1871 | |
1872 | // split curve and try again... |
1873 | const auto halves = bezier.split(); |
1874 | qt_painterpath_isect_curve(bezier: halves.first, pt, winding, depth: depth + 1); |
1875 | qt_painterpath_isect_curve(bezier: halves.second, pt, winding, depth: depth + 1); |
1876 | } |
1877 | } |
1878 | |
1879 | /*! |
1880 | \fn bool QPainterPath::contains(const QPointF &point) const |
1881 | |
1882 | Returns \c true if the given \a point is inside the path, otherwise |
1883 | returns \c false. |
1884 | |
1885 | \sa intersects() |
1886 | */ |
1887 | bool QPainterPath::contains(const QPointF &pt) const |
1888 | { |
1889 | if (isEmpty() || !controlPointRect().contains(p: pt)) |
1890 | return false; |
1891 | |
1892 | QPainterPathData *d = d_func(); |
1893 | |
1894 | int winding_number = 0; |
1895 | |
1896 | QPointF last_pt; |
1897 | QPointF last_start; |
1898 | for (int i=0; i<d->elements.size(); ++i) { |
1899 | const Element &e = d->elements.at(i); |
1900 | |
1901 | switch (e.type) { |
1902 | |
1903 | case MoveToElement: |
1904 | if (i > 0) // implicitly close all paths. |
1905 | qt_painterpath_isect_line(p1: last_pt, p2: last_start, pos: pt, winding: &winding_number); |
1906 | last_start = last_pt = e; |
1907 | break; |
1908 | |
1909 | case LineToElement: |
1910 | qt_painterpath_isect_line(p1: last_pt, p2: e, pos: pt, winding: &winding_number); |
1911 | last_pt = e; |
1912 | break; |
1913 | |
1914 | case CurveToElement: |
1915 | { |
1916 | const QPainterPath::Element &cp2 = d->elements.at(i: ++i); |
1917 | const QPainterPath::Element &ep = d->elements.at(i: ++i); |
1918 | qt_painterpath_isect_curve(bezier: QBezier::fromPoints(p1: last_pt, p2: e, p3: cp2, p4: ep), |
1919 | pt, winding: &winding_number); |
1920 | last_pt = ep; |
1921 | |
1922 | } |
1923 | break; |
1924 | |
1925 | default: |
1926 | break; |
1927 | } |
1928 | } |
1929 | |
1930 | // implicitly close last subpath |
1931 | if (last_pt != last_start) |
1932 | qt_painterpath_isect_line(p1: last_pt, p2: last_start, pos: pt, winding: &winding_number); |
1933 | |
1934 | return (d->fillRule == Qt::WindingFill |
1935 | ? (winding_number != 0) |
1936 | : ((winding_number % 2) != 0)); |
1937 | } |
1938 | |
1939 | enum PainterDirections { Left, Right, Top, Bottom }; |
1940 | |
1941 | static bool qt_painterpath_isect_line_rect(qreal x1, qreal y1, qreal x2, qreal y2, |
1942 | const QRectF &rect) |
1943 | { |
1944 | qreal left = rect.left(); |
1945 | qreal right = rect.right(); |
1946 | qreal top = rect.top(); |
1947 | qreal bottom = rect.bottom(); |
1948 | |
1949 | // clip the lines, after cohen-sutherland, see e.g. http://www.nondot.org/~sabre/graphpro/line6.html |
1950 | int p1 = ((x1 < left) << Left) |
1951 | | ((x1 > right) << Right) |
1952 | | ((y1 < top) << Top) |
1953 | | ((y1 > bottom) << Bottom); |
1954 | int p2 = ((x2 < left) << Left) |
1955 | | ((x2 > right) << Right) |
1956 | | ((y2 < top) << Top) |
1957 | | ((y2 > bottom) << Bottom); |
1958 | |
1959 | if (p1 & p2) |
1960 | // completely inside |
1961 | return false; |
1962 | |
1963 | if (p1 | p2) { |
1964 | qreal dx = x2 - x1; |
1965 | qreal dy = y2 - y1; |
1966 | |
1967 | // clip x coordinates |
1968 | if (x1 < left) { |
1969 | y1 += dy/dx * (left - x1); |
1970 | x1 = left; |
1971 | } else if (x1 > right) { |
1972 | y1 -= dy/dx * (x1 - right); |
1973 | x1 = right; |
1974 | } |
1975 | if (x2 < left) { |
1976 | y2 += dy/dx * (left - x2); |
1977 | x2 = left; |
1978 | } else if (x2 > right) { |
1979 | y2 -= dy/dx * (x2 - right); |
1980 | x2 = right; |
1981 | } |
1982 | |
1983 | p1 = ((y1 < top) << Top) |
1984 | | ((y1 > bottom) << Bottom); |
1985 | p2 = ((y2 < top) << Top) |
1986 | | ((y2 > bottom) << Bottom); |
1987 | |
1988 | if (p1 & p2) |
1989 | return false; |
1990 | |
1991 | // clip y coordinates |
1992 | if (y1 < top) { |
1993 | x1 += dx/dy * (top - y1); |
1994 | y1 = top; |
1995 | } else if (y1 > bottom) { |
1996 | x1 -= dx/dy * (y1 - bottom); |
1997 | y1 = bottom; |
1998 | } |
1999 | if (y2 < top) { |
2000 | x2 += dx/dy * (top - y2); |
2001 | y2 = top; |
2002 | } else if (y2 > bottom) { |
2003 | x2 -= dx/dy * (y2 - bottom); |
2004 | y2 = bottom; |
2005 | } |
2006 | |
2007 | p1 = ((x1 < left) << Left) |
2008 | | ((x1 > right) << Right); |
2009 | p2 = ((x2 < left) << Left) |
2010 | | ((x2 > right) << Right); |
2011 | |
2012 | if (p1 & p2) |
2013 | return false; |
2014 | |
2015 | return true; |
2016 | } |
2017 | return false; |
2018 | } |
2019 | |
2020 | static bool qt_isect_curve_horizontal(const QBezier &bezier, qreal y, qreal x1, qreal x2, int depth = 0) |
2021 | { |
2022 | QRectF bounds = bezier.bounds(); |
2023 | |
2024 | if (y >= bounds.top() && y < bounds.bottom() |
2025 | && bounds.right() >= x1 && bounds.left() < x2) { |
2026 | const qreal lower_bound = qreal(.01); |
2027 | if (depth == 32 || (bounds.width() < lower_bound && bounds.height() < lower_bound)) |
2028 | return true; |
2029 | |
2030 | const auto halves = bezier.split(); |
2031 | if (qt_isect_curve_horizontal(bezier: halves.first, y, x1, x2, depth: depth + 1) |
2032 | || qt_isect_curve_horizontal(bezier: halves.second, y, x1, x2, depth: depth + 1)) |
2033 | return true; |
2034 | } |
2035 | return false; |
2036 | } |
2037 | |
2038 | static bool qt_isect_curve_vertical(const QBezier &bezier, qreal x, qreal y1, qreal y2, int depth = 0) |
2039 | { |
2040 | QRectF bounds = bezier.bounds(); |
2041 | |
2042 | if (x >= bounds.left() && x < bounds.right() |
2043 | && bounds.bottom() >= y1 && bounds.top() < y2) { |
2044 | const qreal lower_bound = qreal(.01); |
2045 | if (depth == 32 || (bounds.width() < lower_bound && bounds.height() < lower_bound)) |
2046 | return true; |
2047 | |
2048 | const auto halves = bezier.split(); |
2049 | if (qt_isect_curve_vertical(bezier: halves.first, x, y1, y2, depth: depth + 1) |
2050 | || qt_isect_curve_vertical(bezier: halves.second, x, y1, y2, depth: depth + 1)) |
2051 | return true; |
2052 | } |
2053 | return false; |
2054 | } |
2055 | |
2056 | static bool pointOnEdge(const QRectF &rect, const QPointF &point) |
2057 | { |
2058 | if ((point.x() == rect.left() || point.x() == rect.right()) && |
2059 | (point.y() >= rect.top() && point.y() <= rect.bottom())) |
2060 | return true; |
2061 | if ((point.y() == rect.top() || point.y() == rect.bottom()) && |
2062 | (point.x() >= rect.left() && point.x() <= rect.right())) |
2063 | return true; |
2064 | return false; |
2065 | } |
2066 | |
2067 | /* |
2068 | Returns \c true if any lines or curves cross the four edges in of rect |
2069 | */ |
2070 | static bool qt_painterpath_check_crossing(const QPainterPath *path, const QRectF &rect) |
2071 | { |
2072 | QPointF last_pt; |
2073 | QPointF last_start; |
2074 | enum { OnRect, InsideRect, OutsideRect} edgeStatus = OnRect; |
2075 | for (int i=0; i<path->elementCount(); ++i) { |
2076 | const QPainterPath::Element &e = path->elementAt(i); |
2077 | |
2078 | switch (e.type) { |
2079 | |
2080 | case QPainterPath::MoveToElement: |
2081 | if (i > 0 |
2082 | && qFuzzyCompare(p1: last_pt.x(), p2: last_start.x()) |
2083 | && qFuzzyCompare(p1: last_pt.y(), p2: last_start.y()) |
2084 | && qt_painterpath_isect_line_rect(x1: last_pt.x(), y1: last_pt.y(), |
2085 | x2: last_start.x(), y2: last_start.y(), rect)) |
2086 | return true; |
2087 | last_start = last_pt = e; |
2088 | break; |
2089 | |
2090 | case QPainterPath::LineToElement: |
2091 | if (qt_painterpath_isect_line_rect(x1: last_pt.x(), y1: last_pt.y(), x2: e.x, y2: e.y, rect)) |
2092 | return true; |
2093 | last_pt = e; |
2094 | break; |
2095 | |
2096 | case QPainterPath::CurveToElement: |
2097 | { |
2098 | QPointF cp2 = path->elementAt(i: ++i); |
2099 | QPointF ep = path->elementAt(i: ++i); |
2100 | QBezier bezier = QBezier::fromPoints(p1: last_pt, p2: e, p3: cp2, p4: ep); |
2101 | if (qt_isect_curve_horizontal(bezier, y: rect.top(), x1: rect.left(), x2: rect.right()) |
2102 | || qt_isect_curve_horizontal(bezier, y: rect.bottom(), x1: rect.left(), x2: rect.right()) |
2103 | || qt_isect_curve_vertical(bezier, x: rect.left(), y1: rect.top(), y2: rect.bottom()) |
2104 | || qt_isect_curve_vertical(bezier, x: rect.right(), y1: rect.top(), y2: rect.bottom())) |
2105 | return true; |
2106 | last_pt = ep; |
2107 | } |
2108 | break; |
2109 | |
2110 | default: |
2111 | break; |
2112 | } |
2113 | // Handle crossing the edges of the rect at the end-points of individual sub-paths. |
2114 | // A point on on the edge itself is considered neither inside nor outside for this purpose. |
2115 | if (!pointOnEdge(rect, point: last_pt)) { |
2116 | bool contained = rect.contains(p: last_pt); |
2117 | switch (edgeStatus) { |
2118 | case OutsideRect: |
2119 | if (contained) |
2120 | return true; |
2121 | break; |
2122 | case InsideRect: |
2123 | if (!contained) |
2124 | return true; |
2125 | break; |
2126 | case OnRect: |
2127 | edgeStatus = contained ? InsideRect : OutsideRect; |
2128 | break; |
2129 | } |
2130 | } else { |
2131 | if (last_pt == last_start) |
2132 | edgeStatus = OnRect; |
2133 | } |
2134 | } |
2135 | |
2136 | // implicitly close last subpath |
2137 | if (last_pt != last_start |
2138 | && qt_painterpath_isect_line_rect(x1: last_pt.x(), y1: last_pt.y(), |
2139 | x2: last_start.x(), y2: last_start.y(), rect)) |
2140 | return true; |
2141 | |
2142 | return false; |
2143 | } |
2144 | |
2145 | /*! |
2146 | \fn bool QPainterPath::intersects(const QRectF &rectangle) const |
2147 | |
2148 | Returns \c true if any point in the given \a rectangle intersects the |
2149 | path; otherwise returns \c false. |
2150 | |
2151 | There is an intersection if any of the lines making up the |
2152 | rectangle crosses a part of the path or if any part of the |
2153 | rectangle overlaps with any area enclosed by the path. This |
2154 | function respects the current fillRule to determine what is |
2155 | considered inside the path. |
2156 | |
2157 | \sa contains() |
2158 | */ |
2159 | bool QPainterPath::intersects(const QRectF &rect) const |
2160 | { |
2161 | if (elementCount() == 1 && rect.contains(p: elementAt(i: 0))) |
2162 | return true; |
2163 | |
2164 | if (isEmpty()) |
2165 | return false; |
2166 | |
2167 | QRectF cp = controlPointRect(); |
2168 | QRectF rn = rect.normalized(); |
2169 | |
2170 | // QRectF::intersects returns false if one of the rects is a null rect |
2171 | // which would happen for a painter path consisting of a vertical or |
2172 | // horizontal line |
2173 | if (qMax(a: rn.left(), b: cp.left()) > qMin(a: rn.right(), b: cp.right()) |
2174 | || qMax(a: rn.top(), b: cp.top()) > qMin(a: rn.bottom(), b: cp.bottom())) |
2175 | return false; |
2176 | |
2177 | // If any path element cross the rect its bound to be an intersection |
2178 | if (qt_painterpath_check_crossing(path: this, rect)) |
2179 | return true; |
2180 | |
2181 | if (contains(pt: rect.center())) |
2182 | return true; |
2183 | |
2184 | Q_D(QPainterPath); |
2185 | |
2186 | // Check if the rectangle surounds any subpath... |
2187 | for (int i=0; i<d->elements.size(); ++i) { |
2188 | const Element &e = d->elements.at(i); |
2189 | if (e.type == QPainterPath::MoveToElement && rect.contains(p: e)) |
2190 | return true; |
2191 | } |
2192 | |
2193 | return false; |
2194 | } |
2195 | |
2196 | /*! |
2197 | Translates all elements in the path by (\a{dx}, \a{dy}). |
2198 | |
2199 | \since 4.6 |
2200 | \sa translated() |
2201 | */ |
2202 | void QPainterPath::translate(qreal dx, qreal dy) |
2203 | { |
2204 | if (!d_ptr || (dx == 0 && dy == 0)) |
2205 | return; |
2206 | |
2207 | int elementsLeft = d_ptr->elements.size(); |
2208 | if (elementsLeft <= 0) |
2209 | return; |
2210 | |
2211 | detach(); |
2212 | QPainterPath::Element *element = d_func()->elements.data(); |
2213 | Q_ASSERT(element); |
2214 | while (elementsLeft--) { |
2215 | element->x += dx; |
2216 | element->y += dy; |
2217 | ++element; |
2218 | } |
2219 | } |
2220 | |
2221 | /*! |
2222 | \fn void QPainterPath::translate(const QPointF &offset) |
2223 | \overload |
2224 | \since 4.6 |
2225 | |
2226 | Translates all elements in the path by the given \a offset. |
2227 | |
2228 | \sa translated() |
2229 | */ |
2230 | |
2231 | /*! |
2232 | Returns a copy of the path that is translated by (\a{dx}, \a{dy}). |
2233 | |
2234 | \since 4.6 |
2235 | \sa translate() |
2236 | */ |
2237 | QPainterPath QPainterPath::translated(qreal dx, qreal dy) const |
2238 | { |
2239 | QPainterPath copy(*this); |
2240 | copy.translate(dx, dy); |
2241 | return copy; |
2242 | } |
2243 | |
2244 | /*! |
2245 | \fn QPainterPath QPainterPath::translated(const QPointF &offset) const; |
2246 | \overload |
2247 | \since 4.6 |
2248 | |
2249 | Returns a copy of the path that is translated by the given \a offset. |
2250 | |
2251 | \sa translate() |
2252 | */ |
2253 | |
2254 | /*! |
2255 | \fn bool QPainterPath::contains(const QRectF &rectangle) const |
2256 | |
2257 | Returns \c true if the given \a rectangle is inside the path, |
2258 | otherwise returns \c false. |
2259 | */ |
2260 | bool QPainterPath::contains(const QRectF &rect) const |
2261 | { |
2262 | Q_D(QPainterPath); |
2263 | |
2264 | // the path is empty or the control point rect doesn't completely |
2265 | // cover the rectangle we abort stratight away. |
2266 | if (isEmpty() || !controlPointRect().contains(r: rect)) |
2267 | return false; |
2268 | |
2269 | // if there are intersections, chances are that the rect is not |
2270 | // contained, except if we have winding rule, in which case it |
2271 | // still might. |
2272 | if (qt_painterpath_check_crossing(path: this, rect)) { |
2273 | if (fillRule() == Qt::OddEvenFill) { |
2274 | return false; |
2275 | } else { |
2276 | // Do some wague sampling in the winding case. This is not |
2277 | // precise but it should mostly be good enough. |
2278 | if (!contains(pt: rect.topLeft()) || |
2279 | !contains(pt: rect.topRight()) || |
2280 | !contains(pt: rect.bottomRight()) || |
2281 | !contains(pt: rect.bottomLeft())) |
2282 | return false; |
2283 | } |
2284 | } |
2285 | |
2286 | // If there exists a point inside that is not part of the path its |
2287 | // because: rectangle lies completely outside path or a subpath |
2288 | // excludes parts of the rectangle. Both cases mean that the rect |
2289 | // is not contained |
2290 | if (!contains(pt: rect.center())) |
2291 | return false; |
2292 | |
2293 | // If there are any subpaths inside this rectangle we need to |
2294 | // check if they are still contained as a result of the fill |
2295 | // rule. This can only be the case for WindingFill though. For |
2296 | // OddEvenFill the rect will never be contained if it surrounds a |
2297 | // subpath. (the case where two subpaths are completely identical |
2298 | // can be argued but we choose to neglect it). |
2299 | for (int i=0; i<d->elements.size(); ++i) { |
2300 | const Element &e = d->elements.at(i); |
2301 | if (e.type == QPainterPath::MoveToElement && rect.contains(p: e)) { |
2302 | if (fillRule() == Qt::OddEvenFill) |
2303 | return false; |
2304 | |
2305 | bool stop = false; |
2306 | for (; !stop && i<d->elements.size(); ++i) { |
2307 | const Element &el = d->elements.at(i); |
2308 | switch (el.type) { |
2309 | case MoveToElement: |
2310 | stop = true; |
2311 | break; |
2312 | case LineToElement: |
2313 | if (!contains(pt: el)) |
2314 | return false; |
2315 | break; |
2316 | case CurveToElement: |
2317 | if (!contains(pt: d->elements.at(i: i+2))) |
2318 | return false; |
2319 | i += 2; |
2320 | break; |
2321 | default: |
2322 | break; |
2323 | } |
2324 | } |
2325 | |
2326 | // compensate for the last ++i in the inner for |
2327 | --i; |
2328 | } |
2329 | } |
2330 | |
2331 | return true; |
2332 | } |
2333 | |
2334 | static inline bool epsilonCompare(const QPointF &a, const QPointF &b, const QSizeF &epsilon) |
2335 | { |
2336 | return qAbs(t: a.x() - b.x()) <= epsilon.width() |
2337 | && qAbs(t: a.y() - b.y()) <= epsilon.height(); |
2338 | } |
2339 | |
2340 | /*! |
2341 | Returns \c true if this painterpath is equal to the given \a path. |
2342 | |
2343 | Note that comparing paths may involve a per element comparison |
2344 | which can be slow for complex paths. |
2345 | |
2346 | \sa operator!=() |
2347 | */ |
2348 | |
2349 | bool QPainterPath::operator==(const QPainterPath &path) const |
2350 | { |
2351 | QPainterPathData *d = reinterpret_cast<QPainterPathData *>(d_func()); |
2352 | QPainterPathData *other_d = path.d_func(); |
2353 | if (other_d == d) { |
2354 | return true; |
2355 | } else if (!d || !other_d) { |
2356 | if (!other_d && isEmpty() && elementAt(i: 0) == QPointF() && d->fillRule == Qt::OddEvenFill) |
2357 | return true; |
2358 | if (!d && path.isEmpty() && path.elementAt(i: 0) == QPointF() && other_d->fillRule == Qt::OddEvenFill) |
2359 | return true; |
2360 | return false; |
2361 | } |
2362 | else if (d->fillRule != other_d->fillRule) |
2363 | return false; |
2364 | else if (d->elements.size() != other_d->elements.size()) |
2365 | return false; |
2366 | |
2367 | const qreal qt_epsilon = sizeof(qreal) == sizeof(double) ? 1e-12 : qreal(1e-5); |
2368 | |
2369 | QSizeF epsilon = boundingRect().size(); |
2370 | epsilon.rwidth() *= qt_epsilon; |
2371 | epsilon.rheight() *= qt_epsilon; |
2372 | |
2373 | for (int i = 0; i < d->elements.size(); ++i) |
2374 | if (d->elements.at(i).type != other_d->elements.at(i).type |
2375 | || !epsilonCompare(a: d->elements.at(i), b: other_d->elements.at(i), epsilon)) |
2376 | return false; |
2377 | |
2378 | return true; |
2379 | } |
2380 | |
2381 | /*! |
2382 | Returns \c true if this painter path differs from the given \a path. |
2383 | |
2384 | Note that comparing paths may involve a per element comparison |
2385 | which can be slow for complex paths. |
2386 | |
2387 | \sa operator==() |
2388 | */ |
2389 | |
2390 | bool QPainterPath::operator!=(const QPainterPath &path) const |
2391 | { |
2392 | return !(*this==path); |
2393 | } |
2394 | |
2395 | /*! |
2396 | \since 4.5 |
2397 | |
2398 | Returns the intersection of this path and the \a other path. |
2399 | |
2400 | \sa intersected(), operator&=(), united(), operator|() |
2401 | */ |
2402 | QPainterPath QPainterPath::operator&(const QPainterPath &other) const |
2403 | { |
2404 | return intersected(r: other); |
2405 | } |
2406 | |
2407 | /*! |
2408 | \since 4.5 |
2409 | |
2410 | Returns the union of this path and the \a other path. |
2411 | |
2412 | \sa united(), operator|=(), intersected(), operator&() |
2413 | */ |
2414 | QPainterPath QPainterPath::operator|(const QPainterPath &other) const |
2415 | { |
2416 | return united(r: other); |
2417 | } |
2418 | |
2419 | /*! |
2420 | \since 4.5 |
2421 | |
2422 | Returns the union of this path and the \a other path. This function is equivalent |
2423 | to operator|(). |
2424 | |
2425 | \sa united(), operator+=(), operator-() |
2426 | */ |
2427 | QPainterPath QPainterPath::operator+(const QPainterPath &other) const |
2428 | { |
2429 | return united(r: other); |
2430 | } |
2431 | |
2432 | /*! |
2433 | \since 4.5 |
2434 | |
2435 | Subtracts the \a other path from a copy of this path, and returns the copy. |
2436 | |
2437 | \sa subtracted(), operator-=(), operator+() |
2438 | */ |
2439 | QPainterPath QPainterPath::operator-(const QPainterPath &other) const |
2440 | { |
2441 | return subtracted(r: other); |
2442 | } |
2443 | |
2444 | /*! |
2445 | \since 4.5 |
2446 | |
2447 | Intersects this path with \a other and returns a reference to this path. |
2448 | |
2449 | \sa intersected(), operator&(), operator|=() |
2450 | */ |
2451 | QPainterPath &QPainterPath::operator&=(const QPainterPath &other) |
2452 | { |
2453 | return *this = (*this & other); |
2454 | } |
2455 | |
2456 | /*! |
2457 | \since 4.5 |
2458 | |
2459 | Unites this path with \a other and returns a reference to this path. |
2460 | |
2461 | \sa united(), operator|(), operator&=() |
2462 | */ |
2463 | QPainterPath &QPainterPath::operator|=(const QPainterPath &other) |
2464 | { |
2465 | return *this = (*this | other); |
2466 | } |
2467 | |
2468 | /*! |
2469 | \since 4.5 |
2470 | |
2471 | Unites this path with \a other, and returns a reference to this path. This |
2472 | is equivalent to operator|=(). |
2473 | |
2474 | \sa united(), operator+(), operator-=() |
2475 | */ |
2476 | QPainterPath &QPainterPath::operator+=(const QPainterPath &other) |
2477 | { |
2478 | return *this = (*this + other); |
2479 | } |
2480 | |
2481 | /*! |
2482 | \since 4.5 |
2483 | |
2484 | Subtracts \a other from this path, and returns a reference to this |
2485 | path. |
2486 | |
2487 | \sa subtracted(), operator-(), operator+=() |
2488 | */ |
2489 | QPainterPath &QPainterPath::operator-=(const QPainterPath &other) |
2490 | { |
2491 | return *this = (*this - other); |
2492 | } |
2493 | |
2494 | #ifndef QT_NO_DATASTREAM |
2495 | /*! |
2496 | \fn QDataStream &operator<<(QDataStream &stream, const QPainterPath &path) |
2497 | \relates QPainterPath |
2498 | |
2499 | Writes the given painter \a path to the given \a stream, and |
2500 | returns a reference to the \a stream. |
2501 | |
2502 | \sa {Serializing Qt Data Types} |
2503 | */ |
2504 | QDataStream &operator<<(QDataStream &s, const QPainterPath &p) |
2505 | { |
2506 | if (p.isEmpty()) { |
2507 | s << 0; |
2508 | return s; |
2509 | } |
2510 | |
2511 | s << p.elementCount(); |
2512 | for (int i=0; i < p.d_func()->elements.size(); ++i) { |
2513 | const QPainterPath::Element &e = p.d_func()->elements.at(i); |
2514 | s << int(e.type); |
2515 | s << double(e.x) << double(e.y); |
2516 | } |
2517 | s << p.d_func()->cStart; |
2518 | s << int(p.d_func()->fillRule); |
2519 | return s; |
2520 | } |
2521 | |
2522 | /*! |
2523 | \fn QDataStream &operator>>(QDataStream &stream, QPainterPath &path) |
2524 | \relates QPainterPath |
2525 | |
2526 | Reads a painter path from the given \a stream into the specified \a path, |
2527 | and returns a reference to the \a stream. |
2528 | |
2529 | \sa {Serializing Qt Data Types} |
2530 | */ |
2531 | QDataStream &operator>>(QDataStream &s, QPainterPath &p) |
2532 | { |
2533 | bool errorDetected = false; |
2534 | int size; |
2535 | s >> size; |
2536 | |
2537 | if (size == 0) |
2538 | return s; |
2539 | |
2540 | p.ensureData(); // in case if p.d_func() == 0 |
2541 | if (p.d_func()->elements.size() == 1) { |
2542 | Q_ASSERT(p.d_func()->elements.at(0).type == QPainterPath::MoveToElement); |
2543 | p.d_func()->elements.clear(); |
2544 | } |
2545 | for (int i=0; i<size; ++i) { |
2546 | int type; |
2547 | double x, y; |
2548 | s >> type; |
2549 | s >> x; |
2550 | s >> y; |
2551 | Q_ASSERT(type >= 0 && type <= 3); |
2552 | if (!isValidCoord(c: qreal(x)) || !isValidCoord(c: qreal(y))) { |
2553 | #ifndef QT_NO_DEBUG |
2554 | qWarning(msg: "QDataStream::operator>>: Invalid QPainterPath coordinates read, skipping it" ); |
2555 | #endif |
2556 | errorDetected = true; |
2557 | continue; |
2558 | } |
2559 | QPainterPath::Element elm = { .x: qreal(x), .y: qreal(y), .type: QPainterPath::ElementType(type) }; |
2560 | p.d_func()->elements.append(t: elm); |
2561 | } |
2562 | s >> p.d_func()->cStart; |
2563 | int fillRule; |
2564 | s >> fillRule; |
2565 | Q_ASSERT(fillRule == Qt::OddEvenFill || fillRule == Qt::WindingFill); |
2566 | p.d_func()->fillRule = Qt::FillRule(fillRule); |
2567 | p.d_func()->dirtyBounds = true; |
2568 | p.d_func()->dirtyControlBounds = true; |
2569 | if (errorDetected) |
2570 | p = QPainterPath(); // Better than to return path with possibly corrupt datastructure, which would likely cause crash |
2571 | return s; |
2572 | } |
2573 | #endif // QT_NO_DATASTREAM |
2574 | |
2575 | |
2576 | /******************************************************************************* |
2577 | * class QPainterPathStroker |
2578 | */ |
2579 | |
2580 | void qt_path_stroke_move_to(qfixed x, qfixed y, void *data) |
2581 | { |
2582 | ((QPainterPath *) data)->moveTo(qt_fixed_to_real(x), qt_fixed_to_real(y)); |
2583 | } |
2584 | |
2585 | void qt_path_stroke_line_to(qfixed x, qfixed y, void *data) |
2586 | { |
2587 | ((QPainterPath *) data)->lineTo(qt_fixed_to_real(x), qt_fixed_to_real(y)); |
2588 | } |
2589 | |
2590 | void qt_path_stroke_cubic_to(qfixed c1x, qfixed c1y, |
2591 | qfixed c2x, qfixed c2y, |
2592 | qfixed ex, qfixed ey, |
2593 | void *data) |
2594 | { |
2595 | ((QPainterPath *) data)->cubicTo(qt_fixed_to_real(c1x), qt_fixed_to_real(c1y), |
2596 | qt_fixed_to_real(c2x), qt_fixed_to_real(c2y), |
2597 | qt_fixed_to_real(ex), qt_fixed_to_real(ey)); |
2598 | } |
2599 | |
2600 | /*! |
2601 | \since 4.1 |
2602 | \class QPainterPathStroker |
2603 | \ingroup painting |
2604 | \inmodule QtGui |
2605 | |
2606 | \brief The QPainterPathStroker class is used to generate fillable |
2607 | outlines for a given painter path. |
2608 | |
2609 | By calling the createStroke() function, passing a given |
2610 | QPainterPath as argument, a new painter path representing the |
2611 | outline of the given path is created. The newly created painter |
2612 | path can then be filled to draw the original painter path's |
2613 | outline. |
2614 | |
2615 | You can control the various design aspects (width, cap styles, |
2616 | join styles and dash pattern) of the outlining using the following |
2617 | functions: |
2618 | |
2619 | \list |
2620 | \li setWidth() |
2621 | \li setCapStyle() |
2622 | \li setJoinStyle() |
2623 | \li setDashPattern() |
2624 | \endlist |
2625 | |
2626 | The setDashPattern() function accepts both a Qt::PenStyle object |
2627 | and a vector representation of the pattern as argument. |
2628 | |
2629 | In addition you can specify a curve's threshold, controlling the |
2630 | granularity with which a curve is drawn, using the |
2631 | setCurveThreshold() function. The default threshold is a well |
2632 | adjusted value (0.25), and normally you should not need to modify |
2633 | it. However, you can make the curve's appearance smoother by |
2634 | decreasing its value. |
2635 | |
2636 | You can also control the miter limit for the generated outline |
2637 | using the setMiterLimit() function. The miter limit describes how |
2638 | far from each join the miter join can extend. The limit is |
2639 | specified in the units of width so the pixelwise miter limit will |
2640 | be \c {miterlimit * width}. This value is only used if the join |
2641 | style is Qt::MiterJoin. |
2642 | |
2643 | The painter path generated by the createStroke() function should |
2644 | only be used for outlining the given painter path. Otherwise it |
2645 | may cause unexpected behavior. Generated outlines also require the |
2646 | Qt::WindingFill rule which is set by default. |
2647 | |
2648 | \sa QPen, QBrush |
2649 | */ |
2650 | |
2651 | QPainterPathStrokerPrivate::QPainterPathStrokerPrivate() |
2652 | : dashOffset(0) |
2653 | { |
2654 | stroker.setMoveToHook(qt_path_stroke_move_to); |
2655 | stroker.setLineToHook(qt_path_stroke_line_to); |
2656 | stroker.setCubicToHook(qt_path_stroke_cubic_to); |
2657 | } |
2658 | |
2659 | /*! |
2660 | Creates a new stroker. |
2661 | */ |
2662 | QPainterPathStroker::QPainterPathStroker() |
2663 | : d_ptr(new QPainterPathStrokerPrivate) |
2664 | { |
2665 | } |
2666 | |
2667 | /*! |
2668 | Creates a new stroker based on \a pen. |
2669 | |
2670 | \since 5.3 |
2671 | */ |
2672 | QPainterPathStroker::QPainterPathStroker(const QPen &pen) |
2673 | : d_ptr(new QPainterPathStrokerPrivate) |
2674 | { |
2675 | setWidth(pen.widthF()); |
2676 | setCapStyle(pen.capStyle()); |
2677 | setJoinStyle(pen.joinStyle()); |
2678 | setMiterLimit(pen.miterLimit()); |
2679 | setDashOffset(pen.dashOffset()); |
2680 | |
2681 | if (pen.style() == Qt::CustomDashLine) |
2682 | setDashPattern(pen.dashPattern()); |
2683 | else |
2684 | setDashPattern(pen.style()); |
2685 | } |
2686 | |
2687 | /*! |
2688 | Destroys the stroker. |
2689 | */ |
2690 | QPainterPathStroker::~QPainterPathStroker() |
2691 | { |
2692 | } |
2693 | |
2694 | |
2695 | /*! |
2696 | Generates a new path that is a fillable area representing the |
2697 | outline of the given \a path. |
2698 | |
2699 | The various design aspects of the outline are based on the |
2700 | stroker's properties: width(), capStyle(), joinStyle(), |
2701 | dashPattern(), curveThreshold() and miterLimit(). |
2702 | |
2703 | The generated path should only be used for outlining the given |
2704 | painter path. Otherwise it may cause unexpected |
2705 | behavior. Generated outlines also require the Qt::WindingFill rule |
2706 | which is set by default. |
2707 | */ |
2708 | QPainterPath QPainterPathStroker::createStroke(const QPainterPath &path) const |
2709 | { |
2710 | QPainterPathStrokerPrivate *d = const_cast<QPainterPathStrokerPrivate *>(d_func()); |
2711 | QPainterPath stroke; |
2712 | if (path.isEmpty()) |
2713 | return path; |
2714 | if (d->dashPattern.isEmpty()) { |
2715 | d->stroker.strokePath(path, data: &stroke, matrix: QTransform()); |
2716 | } else { |
2717 | QDashStroker dashStroker(&d->stroker); |
2718 | dashStroker.setDashPattern(d->dashPattern); |
2719 | dashStroker.setDashOffset(d->dashOffset); |
2720 | dashStroker.setClipRect(d->stroker.clipRect()); |
2721 | dashStroker.strokePath(path, data: &stroke, matrix: QTransform()); |
2722 | } |
2723 | stroke.setFillRule(Qt::WindingFill); |
2724 | return stroke; |
2725 | } |
2726 | |
2727 | /*! |
2728 | Sets the width of the generated outline painter path to \a width. |
2729 | |
2730 | The generated outlines will extend approximately 50% of \a width |
2731 | to each side of the given input path's original outline. |
2732 | */ |
2733 | void QPainterPathStroker::setWidth(qreal width) |
2734 | { |
2735 | Q_D(QPainterPathStroker); |
2736 | if (width <= 0) |
2737 | width = 1; |
2738 | d->stroker.setStrokeWidth(qt_real_to_fixed(width)); |
2739 | } |
2740 | |
2741 | /*! |
2742 | Returns the width of the generated outlines. |
2743 | */ |
2744 | qreal QPainterPathStroker::width() const |
2745 | { |
2746 | return qt_fixed_to_real(d_func()->stroker.strokeWidth()); |
2747 | } |
2748 | |
2749 | |
2750 | /*! |
2751 | Sets the cap style of the generated outlines to \a style. If a |
2752 | dash pattern is set, each segment of the pattern is subject to the |
2753 | cap \a style. |
2754 | */ |
2755 | void QPainterPathStroker::setCapStyle(Qt::PenCapStyle style) |
2756 | { |
2757 | d_func()->stroker.setCapStyle(style); |
2758 | } |
2759 | |
2760 | |
2761 | /*! |
2762 | Returns the cap style of the generated outlines. |
2763 | */ |
2764 | Qt::PenCapStyle QPainterPathStroker::capStyle() const |
2765 | { |
2766 | return d_func()->stroker.capStyle(); |
2767 | } |
2768 | |
2769 | /*! |
2770 | Sets the join style of the generated outlines to \a style. |
2771 | */ |
2772 | void QPainterPathStroker::setJoinStyle(Qt::PenJoinStyle style) |
2773 | { |
2774 | d_func()->stroker.setJoinStyle(style); |
2775 | } |
2776 | |
2777 | /*! |
2778 | Returns the join style of the generated outlines. |
2779 | */ |
2780 | Qt::PenJoinStyle QPainterPathStroker::joinStyle() const |
2781 | { |
2782 | return d_func()->stroker.joinStyle(); |
2783 | } |
2784 | |
2785 | /*! |
2786 | Sets the miter limit of the generated outlines to \a limit. |
2787 | |
2788 | The miter limit describes how far from each join the miter join |
2789 | can extend. The limit is specified in units of the currently set |
2790 | width. So the pixelwise miter limit will be \c { miterlimit * |
2791 | width}. |
2792 | |
2793 | This value is only used if the join style is Qt::MiterJoin. |
2794 | */ |
2795 | void QPainterPathStroker::setMiterLimit(qreal limit) |
2796 | { |
2797 | d_func()->stroker.setMiterLimit(qt_real_to_fixed(limit)); |
2798 | } |
2799 | |
2800 | /*! |
2801 | Returns the miter limit for the generated outlines. |
2802 | */ |
2803 | qreal QPainterPathStroker::miterLimit() const |
2804 | { |
2805 | return qt_fixed_to_real(d_func()->stroker.miterLimit()); |
2806 | } |
2807 | |
2808 | |
2809 | /*! |
2810 | Specifies the curve flattening \a threshold, controlling the |
2811 | granularity with which the generated outlines' curve is drawn. |
2812 | |
2813 | The default threshold is a well adjusted value (0.25), and |
2814 | normally you should not need to modify it. However, you can make |
2815 | the curve's appearance smoother by decreasing its value. |
2816 | */ |
2817 | void QPainterPathStroker::setCurveThreshold(qreal threshold) |
2818 | { |
2819 | d_func()->stroker.setCurveThreshold(qt_real_to_fixed(threshold)); |
2820 | } |
2821 | |
2822 | /*! |
2823 | Returns the curve flattening threshold for the generated |
2824 | outlines. |
2825 | */ |
2826 | qreal QPainterPathStroker::curveThreshold() const |
2827 | { |
2828 | return qt_fixed_to_real(d_func()->stroker.curveThreshold()); |
2829 | } |
2830 | |
2831 | /*! |
2832 | Sets the dash pattern for the generated outlines to \a style. |
2833 | */ |
2834 | void QPainterPathStroker::setDashPattern(Qt::PenStyle style) |
2835 | { |
2836 | d_func()->dashPattern = QDashStroker::patternForStyle(style); |
2837 | } |
2838 | |
2839 | /*! |
2840 | \overload |
2841 | |
2842 | Sets the dash pattern for the generated outlines to \a |
2843 | dashPattern. This function makes it possible to specify custom |
2844 | dash patterns. |
2845 | |
2846 | Each element in the vector contains the lengths of the dashes and spaces |
2847 | in the stroke, beginning with the first dash in the first element, the |
2848 | first space in the second element, and alternating between dashes and |
2849 | spaces for each following pair of elements. |
2850 | |
2851 | The vector can contain an odd number of elements, in which case the last |
2852 | element will be extended by the length of the first element when the |
2853 | pattern repeats. |
2854 | */ |
2855 | void QPainterPathStroker::setDashPattern(const QVector<qreal> &dashPattern) |
2856 | { |
2857 | d_func()->dashPattern.clear(); |
2858 | for (int i=0; i<dashPattern.size(); ++i) |
2859 | d_func()->dashPattern << qt_real_to_fixed(dashPattern.at(i)); |
2860 | } |
2861 | |
2862 | /*! |
2863 | Returns the dash pattern for the generated outlines. |
2864 | */ |
2865 | QVector<qreal> QPainterPathStroker::dashPattern() const |
2866 | { |
2867 | return d_func()->dashPattern; |
2868 | } |
2869 | |
2870 | /*! |
2871 | Returns the dash offset for the generated outlines. |
2872 | */ |
2873 | qreal QPainterPathStroker::dashOffset() const |
2874 | { |
2875 | return d_func()->dashOffset; |
2876 | } |
2877 | |
2878 | /*! |
2879 | Sets the dash offset for the generated outlines to \a offset. |
2880 | |
2881 | See the documentation for QPen::setDashOffset() for a description of the |
2882 | dash offset. |
2883 | */ |
2884 | void QPainterPathStroker::setDashOffset(qreal offset) |
2885 | { |
2886 | d_func()->dashOffset = offset; |
2887 | } |
2888 | |
2889 | /*! |
2890 | Converts the path into a polygon using the QTransform |
2891 | \a matrix, and returns the polygon. |
2892 | |
2893 | The polygon is created by first converting all subpaths to |
2894 | polygons, then using a rewinding technique to make sure that |
2895 | overlapping subpaths can be filled using the correct fill rule. |
2896 | |
2897 | Note that rewinding inserts addition lines in the polygon so |
2898 | the outline of the fill polygon does not match the outline of |
2899 | the path. |
2900 | |
2901 | \sa toSubpathPolygons(), toFillPolygons(), |
2902 | {QPainterPath#QPainterPath Conversion}{QPainterPath Conversion} |
2903 | */ |
2904 | QPolygonF QPainterPath::toFillPolygon(const QTransform &matrix) const |
2905 | { |
2906 | |
2907 | const QList<QPolygonF> flats = toSubpathPolygons(matrix); |
2908 | QPolygonF polygon; |
2909 | if (flats.isEmpty()) |
2910 | return polygon; |
2911 | QPointF first = flats.first().first(); |
2912 | for (int i=0; i<flats.size(); ++i) { |
2913 | polygon += flats.at(i); |
2914 | if (!flats.at(i).isClosed()) |
2915 | polygon += flats.at(i).first(); |
2916 | if (i > 0) |
2917 | polygon += first; |
2918 | } |
2919 | return polygon; |
2920 | } |
2921 | |
2922 | #if QT_DEPRECATED_SINCE(5, 15) |
2923 | /*! |
2924 | \overload |
2925 | \obsolete |
2926 | |
2927 | Use toFillPolygon(const QTransform &matrix) instead. |
2928 | */ |
2929 | QPolygonF QPainterPath::toFillPolygon(const QMatrix &matrix) const |
2930 | { |
2931 | return toFillPolygon(matrix: QTransform(matrix)); |
2932 | } |
2933 | #endif // QT_DEPRECATED_SINCE(5, 15) |
2934 | |
2935 | //derivative of the equation |
2936 | static inline qreal slopeAt(qreal t, qreal a, qreal b, qreal c, qreal d) |
2937 | { |
2938 | return 3*t*t*(d - 3*c + 3*b - a) + 6*t*(c - 2*b + a) + 3*(b - a); |
2939 | } |
2940 | |
2941 | /*! |
2942 | Returns the length of the current path. |
2943 | */ |
2944 | qreal QPainterPath::length() const |
2945 | { |
2946 | Q_D(QPainterPath); |
2947 | if (isEmpty()) |
2948 | return 0; |
2949 | |
2950 | qreal len = 0; |
2951 | for (int i=1; i<d->elements.size(); ++i) { |
2952 | const Element &e = d->elements.at(i); |
2953 | |
2954 | switch (e.type) { |
2955 | case MoveToElement: |
2956 | break; |
2957 | case LineToElement: |
2958 | { |
2959 | len += QLineF(d->elements.at(i: i-1), e).length(); |
2960 | break; |
2961 | } |
2962 | case CurveToElement: |
2963 | { |
2964 | QBezier b = QBezier::fromPoints(p1: d->elements.at(i: i-1), |
2965 | p2: e, |
2966 | p3: d->elements.at(i: i+1), |
2967 | p4: d->elements.at(i: i+2)); |
2968 | len += b.length(); |
2969 | i += 2; |
2970 | break; |
2971 | } |
2972 | default: |
2973 | break; |
2974 | } |
2975 | } |
2976 | return len; |
2977 | } |
2978 | |
2979 | /*! |
2980 | Returns percentage of the whole path at the specified length \a len. |
2981 | |
2982 | Note that similarly to other percent methods, the percentage measurement |
2983 | is not linear with regards to the length, if curves are present |
2984 | in the path. When curves are present the percentage argument is mapped |
2985 | to the t parameter of the Bezier equations. |
2986 | */ |
2987 | qreal QPainterPath::percentAtLength(qreal len) const |
2988 | { |
2989 | Q_D(QPainterPath); |
2990 | if (isEmpty() || len <= 0) |
2991 | return 0; |
2992 | |
2993 | qreal totalLength = length(); |
2994 | if (len > totalLength) |
2995 | return 1; |
2996 | |
2997 | qreal curLen = 0; |
2998 | for (int i=1; i<d->elements.size(); ++i) { |
2999 | const Element &e = d->elements.at(i); |
3000 | |
3001 | switch (e.type) { |
3002 | case MoveToElement: |
3003 | break; |
3004 | case LineToElement: |
3005 | { |
3006 | QLineF line(d->elements.at(i: i-1), e); |
3007 | qreal llen = line.length(); |
3008 | curLen += llen; |
3009 | if (curLen >= len) { |
3010 | return len/totalLength ; |
3011 | } |
3012 | |
3013 | break; |
3014 | } |
3015 | case CurveToElement: |
3016 | { |
3017 | QBezier b = QBezier::fromPoints(p1: d->elements.at(i: i-1), |
3018 | p2: e, |
3019 | p3: d->elements.at(i: i+1), |
3020 | p4: d->elements.at(i: i+2)); |
3021 | qreal blen = b.length(); |
3022 | qreal prevLen = curLen; |
3023 | curLen += blen; |
3024 | |
3025 | if (curLen >= len) { |
3026 | qreal res = b.tAtLength(len: len - prevLen); |
3027 | return (res * blen + prevLen)/totalLength; |
3028 | } |
3029 | |
3030 | i += 2; |
3031 | break; |
3032 | } |
3033 | default: |
3034 | break; |
3035 | } |
3036 | } |
3037 | |
3038 | return 0; |
3039 | } |
3040 | |
3041 | static inline QBezier bezierAtT(const QPainterPath &path, qreal t, qreal *startingLength, qreal *bezierLength) |
3042 | { |
3043 | *startingLength = 0; |
3044 | if (t > 1) |
3045 | return QBezier(); |
3046 | |
3047 | qreal curLen = 0; |
3048 | qreal totalLength = path.length(); |
3049 | |
3050 | const int lastElement = path.elementCount() - 1; |
3051 | for (int i=0; i <= lastElement; ++i) { |
3052 | const QPainterPath::Element &e = path.elementAt(i); |
3053 | |
3054 | switch (e.type) { |
3055 | case QPainterPath::MoveToElement: |
3056 | break; |
3057 | case QPainterPath::LineToElement: |
3058 | { |
3059 | QLineF line(path.elementAt(i: i-1), e); |
3060 | qreal llen = line.length(); |
3061 | curLen += llen; |
3062 | if (i == lastElement || curLen/totalLength >= t) { |
3063 | *bezierLength = llen; |
3064 | QPointF a = path.elementAt(i: i-1); |
3065 | QPointF delta = e - a; |
3066 | return QBezier::fromPoints(p1: a, p2: a + delta / 3, p3: a + 2 * delta / 3, p4: e); |
3067 | } |
3068 | break; |
3069 | } |
3070 | case QPainterPath::CurveToElement: |
3071 | { |
3072 | QBezier b = QBezier::fromPoints(p1: path.elementAt(i: i-1), |
3073 | p2: e, |
3074 | p3: path.elementAt(i: i+1), |
3075 | p4: path.elementAt(i: i+2)); |
3076 | qreal blen = b.length(); |
3077 | curLen += blen; |
3078 | |
3079 | if (i + 2 == lastElement || curLen/totalLength >= t) { |
3080 | *bezierLength = blen; |
3081 | return b; |
3082 | } |
3083 | |
3084 | i += 2; |
3085 | break; |
3086 | } |
3087 | default: |
3088 | break; |
3089 | } |
3090 | *startingLength = curLen; |
3091 | } |
3092 | return QBezier(); |
3093 | } |
3094 | |
3095 | /*! |
3096 | Returns the point at at the percentage \a t of the current path. |
3097 | The argument \a t has to be between 0 and 1. |
3098 | |
3099 | Note that similarly to other percent methods, the percentage measurement |
3100 | is not linear with regards to the length, if curves are present |
3101 | in the path. When curves are present the percentage argument is mapped |
3102 | to the t parameter of the Bezier equations. |
3103 | */ |
3104 | QPointF QPainterPath::pointAtPercent(qreal t) const |
3105 | { |
3106 | if (t < 0 || t > 1) { |
3107 | qWarning(msg: "QPainterPath::pointAtPercent accepts only values between 0 and 1" ); |
3108 | return QPointF(); |
3109 | } |
3110 | |
3111 | if (!d_ptr || d_ptr->elements.size() == 0) |
3112 | return QPointF(); |
3113 | |
3114 | if (d_ptr->elements.size() == 1) |
3115 | return d_ptr->elements.at(i: 0); |
3116 | |
3117 | qreal totalLength = length(); |
3118 | qreal curLen = 0; |
3119 | qreal bezierLen = 0; |
3120 | QBezier b = bezierAtT(path: *this, t, startingLength: &curLen, bezierLength: &bezierLen); |
3121 | qreal realT = (totalLength * t - curLen) / bezierLen; |
3122 | |
3123 | return b.pointAt(t: qBound(min: qreal(0), val: realT, max: qreal(1))); |
3124 | } |
3125 | |
3126 | /*! |
3127 | Returns the angle of the path tangent at the percentage \a t. |
3128 | The argument \a t has to be between 0 and 1. |
3129 | |
3130 | Positive values for the angles mean counter-clockwise while negative values |
3131 | mean the clockwise direction. Zero degrees is at the 3 o'clock position. |
3132 | |
3133 | Note that similarly to the other percent methods, the percentage measurement |
3134 | is not linear with regards to the length if curves are present |
3135 | in the path. When curves are present the percentage argument is mapped |
3136 | to the t parameter of the Bezier equations. |
3137 | */ |
3138 | qreal QPainterPath::angleAtPercent(qreal t) const |
3139 | { |
3140 | if (t < 0 || t > 1) { |
3141 | qWarning(msg: "QPainterPath::angleAtPercent accepts only values between 0 and 1" ); |
3142 | return 0; |
3143 | } |
3144 | |
3145 | qreal totalLength = length(); |
3146 | qreal curLen = 0; |
3147 | qreal bezierLen = 0; |
3148 | QBezier bez = bezierAtT(path: *this, t, startingLength: &curLen, bezierLength: &bezierLen); |
3149 | qreal realT = (totalLength * t - curLen) / bezierLen; |
3150 | |
3151 | qreal m1 = slopeAt(t: realT, a: bez.x1, b: bez.x2, c: bez.x3, d: bez.x4); |
3152 | qreal m2 = slopeAt(t: realT, a: bez.y1, b: bez.y2, c: bez.y3, d: bez.y4); |
3153 | |
3154 | return QLineF(0, 0, m1, m2).angle(); |
3155 | } |
3156 | |
3157 | |
3158 | /*! |
3159 | Returns the slope of the path at the percentage \a t. The |
3160 | argument \a t has to be between 0 and 1. |
3161 | |
3162 | Note that similarly to other percent methods, the percentage measurement |
3163 | is not linear with regards to the length, if curves are present |
3164 | in the path. When curves are present the percentage argument is mapped |
3165 | to the t parameter of the Bezier equations. |
3166 | */ |
3167 | qreal QPainterPath::slopeAtPercent(qreal t) const |
3168 | { |
3169 | if (t < 0 || t > 1) { |
3170 | qWarning(msg: "QPainterPath::slopeAtPercent accepts only values between 0 and 1" ); |
3171 | return 0; |
3172 | } |
3173 | |
3174 | qreal totalLength = length(); |
3175 | qreal curLen = 0; |
3176 | qreal bezierLen = 0; |
3177 | QBezier bez = bezierAtT(path: *this, t, startingLength: &curLen, bezierLength: &bezierLen); |
3178 | qreal realT = (totalLength * t - curLen) / bezierLen; |
3179 | |
3180 | qreal m1 = slopeAt(t: realT, a: bez.x1, b: bez.x2, c: bez.x3, d: bez.x4); |
3181 | qreal m2 = slopeAt(t: realT, a: bez.y1, b: bez.y2, c: bez.y3, d: bez.y4); |
3182 | //tangent line |
3183 | qreal slope = 0; |
3184 | |
3185 | if (m1) |
3186 | slope = m2/m1; |
3187 | else { |
3188 | if (std::numeric_limits<qreal>::has_infinity) { |
3189 | slope = (m2 < 0) ? -std::numeric_limits<qreal>::infinity() |
3190 | : std::numeric_limits<qreal>::infinity(); |
3191 | } else { |
3192 | if (sizeof(qreal) == sizeof(double)) { |
3193 | return 1.79769313486231570e+308; |
3194 | } else { |
3195 | return ((qreal)3.40282346638528860e+38); |
3196 | } |
3197 | } |
3198 | } |
3199 | |
3200 | return slope; |
3201 | } |
3202 | |
3203 | /*! |
3204 | \since 4.4 |
3205 | |
3206 | Adds the given rectangle \a rect with rounded corners to the path. |
3207 | |
3208 | The \a xRadius and \a yRadius arguments specify the radii of |
3209 | the ellipses defining the corners of the rounded rectangle. |
3210 | When \a mode is Qt::RelativeSize, \a xRadius and |
3211 | \a yRadius are specified in percentage of half the rectangle's |
3212 | width and height respectively, and should be in the range 0.0 to 100.0. |
3213 | |
3214 | \sa addRect() |
3215 | */ |
3216 | void QPainterPath::addRoundedRect(const QRectF &rect, qreal xRadius, qreal yRadius, |
3217 | Qt::SizeMode mode) |
3218 | { |
3219 | QRectF r = rect.normalized(); |
3220 | |
3221 | if (r.isNull()) |
3222 | return; |
3223 | |
3224 | if (mode == Qt::AbsoluteSize) { |
3225 | qreal w = r.width() / 2; |
3226 | qreal h = r.height() / 2; |
3227 | |
3228 | if (w == 0) { |
3229 | xRadius = 0; |
3230 | } else { |
3231 | xRadius = 100 * qMin(a: xRadius, b: w) / w; |
3232 | } |
3233 | if (h == 0) { |
3234 | yRadius = 0; |
3235 | } else { |
3236 | yRadius = 100 * qMin(a: yRadius, b: h) / h; |
3237 | } |
3238 | } else { |
3239 | if (xRadius > 100) // fix ranges |
3240 | xRadius = 100; |
3241 | |
3242 | if (yRadius > 100) |
3243 | yRadius = 100; |
3244 | } |
3245 | |
3246 | if (xRadius <= 0 || yRadius <= 0) { // add normal rectangle |
3247 | addRect(r); |
3248 | return; |
3249 | } |
3250 | |
3251 | qreal x = r.x(); |
3252 | qreal y = r.y(); |
3253 | qreal w = r.width(); |
3254 | qreal h = r.height(); |
3255 | qreal rxx2 = w*xRadius/100; |
3256 | qreal ryy2 = h*yRadius/100; |
3257 | |
3258 | ensureData(); |
3259 | detach(); |
3260 | |
3261 | bool first = d_func()->elements.size() < 2; |
3262 | |
3263 | arcMoveTo(x, y, w: rxx2, h: ryy2, angle: 180); |
3264 | arcTo(x, y, w: rxx2, h: ryy2, startAngle: 180, arcLength: -90); |
3265 | arcTo(x: x+w-rxx2, y, w: rxx2, h: ryy2, startAngle: 90, arcLength: -90); |
3266 | arcTo(x: x+w-rxx2, y: y+h-ryy2, w: rxx2, h: ryy2, startAngle: 0, arcLength: -90); |
3267 | arcTo(x, y: y+h-ryy2, w: rxx2, h: ryy2, startAngle: 270, arcLength: -90); |
3268 | closeSubpath(); |
3269 | |
3270 | d_func()->require_moveTo = true; |
3271 | d_func()->convex = first; |
3272 | } |
3273 | |
3274 | /*! |
3275 | \fn void QPainterPath::addRoundedRect(qreal x, qreal y, qreal w, qreal h, qreal xRadius, qreal yRadius, Qt::SizeMode mode = Qt::AbsoluteSize); |
3276 | \since 4.4 |
3277 | \overload |
3278 | |
3279 | Adds the given rectangle \a x, \a y, \a w, \a h with rounded corners to the path. |
3280 | */ |
3281 | |
3282 | #if QT_DEPRECATED_SINCE(5, 13) |
3283 | /*! |
3284 | \obsolete |
3285 | |
3286 | Adds a rectangle \a r with rounded corners to the path. |
3287 | |
3288 | The \a xRnd and \a yRnd arguments specify how rounded the corners |
3289 | should be. 0 is angled corners, 99 is maximum roundedness. |
3290 | |
3291 | \sa addRoundedRect() |
3292 | */ |
3293 | void QPainterPath::addRoundRect(const QRectF &r, int xRnd, int yRnd) |
3294 | { |
3295 | if(xRnd >= 100) // fix ranges |
3296 | xRnd = 99; |
3297 | if(yRnd >= 100) |
3298 | yRnd = 99; |
3299 | if(xRnd <= 0 || yRnd <= 0) { // add normal rectangle |
3300 | addRect(r); |
3301 | return; |
3302 | } |
3303 | |
3304 | QRectF rect = r.normalized(); |
3305 | |
3306 | if (rect.isNull()) |
3307 | return; |
3308 | |
3309 | qreal x = rect.x(); |
3310 | qreal y = rect.y(); |
3311 | qreal w = rect.width(); |
3312 | qreal h = rect.height(); |
3313 | qreal rxx2 = w*xRnd/100; |
3314 | qreal ryy2 = h*yRnd/100; |
3315 | |
3316 | ensureData(); |
3317 | detach(); |
3318 | |
3319 | bool first = d_func()->elements.size() < 2; |
3320 | |
3321 | arcMoveTo(x, y, w: rxx2, h: ryy2, angle: 180); |
3322 | arcTo(x, y, w: rxx2, h: ryy2, startAngle: 180, arcLength: -90); |
3323 | arcTo(x: x+w-rxx2, y, w: rxx2, h: ryy2, startAngle: 90, arcLength: -90); |
3324 | arcTo(x: x+w-rxx2, y: y+h-ryy2, w: rxx2, h: ryy2, startAngle: 0, arcLength: -90); |
3325 | arcTo(x, y: y+h-ryy2, w: rxx2, h: ryy2, startAngle: 270, arcLength: -90); |
3326 | closeSubpath(); |
3327 | |
3328 | d_func()->require_moveTo = true; |
3329 | d_func()->convex = first; |
3330 | } |
3331 | |
3332 | /*! |
3333 | \obsolete |
3334 | |
3335 | \fn bool QPainterPath::addRoundRect(const QRectF &rect, int roundness); |
3336 | \since 4.3 |
3337 | \overload |
3338 | |
3339 | Adds a rounded rectangle, \a rect, to the path. |
3340 | |
3341 | The \a roundness argument specifies uniform roundness for the |
3342 | rectangle. Vertical and horizontal roundness factors will be |
3343 | adjusted accordingly to act uniformly around both axes. Use this |
3344 | method if you want a rectangle equally rounded across both the X and |
3345 | Y axis. |
3346 | |
3347 | \sa addRoundedRect() |
3348 | */ |
3349 | void QPainterPath::addRoundRect(const QRectF &rect, |
3350 | int roundness) |
3351 | { |
3352 | int xRnd = roundness; |
3353 | int yRnd = roundness; |
3354 | if (rect.width() > rect.height()) |
3355 | xRnd = int(roundness * rect.height()/rect.width()); |
3356 | else |
3357 | yRnd = int(roundness * rect.width()/rect.height()); |
3358 | addRoundedRect(rect, xRadius: xRnd, yRadius: yRnd, mode: Qt::RelativeSize); |
3359 | } |
3360 | |
3361 | /*! |
3362 | \obsolete |
3363 | |
3364 | \fn void QPainterPath::addRoundRect(qreal x, qreal y, qreal w, qreal h, int xRnd, int yRnd); |
3365 | \overload |
3366 | |
3367 | Adds a rectangle with rounded corners to the path. The rectangle |
3368 | is constructed from \a x, \a y, and the width and height \a w |
3369 | and \a h. |
3370 | |
3371 | The \a xRnd and \a yRnd arguments specify how rounded the corners |
3372 | should be. 0 is angled corners, 99 is maximum roundedness. |
3373 | |
3374 | \sa addRoundedRect() |
3375 | */ |
3376 | void QPainterPath::addRoundRect(qreal x, qreal y, qreal w, qreal h, |
3377 | int xRnd, int yRnd) |
3378 | { |
3379 | addRoundedRect(rect: QRectF(x, y, w, h), xRadius: xRnd, yRadius: yRnd, mode: Qt::RelativeSize); |
3380 | } |
3381 | |
3382 | /*! |
3383 | \obsolete |
3384 | |
3385 | \fn bool QPainterPath::addRoundRect(qreal x, qreal y, qreal width, qreal height, int roundness); |
3386 | \since 4.3 |
3387 | \overload |
3388 | |
3389 | Adds a rounded rectangle to the path, defined by the coordinates \a |
3390 | x and \a y with the specified \a width and \a height. |
3391 | |
3392 | The \a roundness argument specifies uniform roundness for the |
3393 | rectangle. Vertical and horizontal roundness factors will be |
3394 | adjusted accordingly to act uniformly around both axes. Use this |
3395 | method if you want a rectangle equally rounded across both the X and |
3396 | Y axis. |
3397 | |
3398 | \sa addRoundedRect() |
3399 | */ |
3400 | void QPainterPath::addRoundRect(qreal x, qreal y, qreal w, qreal h, |
3401 | int roundness) |
3402 | { |
3403 | addRoundedRect(rect: QRectF(x, y, w, h), xRadius: roundness, yRadius: Qt::RelativeSize); |
3404 | } |
3405 | #endif |
3406 | |
3407 | /*! |
3408 | \since 4.3 |
3409 | |
3410 | Returns a path which is the union of this path's fill area and \a p's fill area. |
3411 | |
3412 | Set operations on paths will treat the paths as areas. Non-closed |
3413 | paths will be treated as implicitly closed. |
3414 | Bezier curves may be flattened to line segments due to numerical instability of |
3415 | doing bezier curve intersections. |
3416 | |
3417 | \sa intersected(), subtracted() |
3418 | */ |
3419 | QPainterPath QPainterPath::united(const QPainterPath &p) const |
3420 | { |
3421 | if (isEmpty() || p.isEmpty()) |
3422 | return isEmpty() ? p : *this; |
3423 | QPathClipper clipper(*this, p); |
3424 | return clipper.clip(op: QPathClipper::BoolOr); |
3425 | } |
3426 | |
3427 | /*! |
3428 | \since 4.3 |
3429 | |
3430 | Returns a path which is the intersection of this path's fill area and \a p's fill area. |
3431 | Bezier curves may be flattened to line segments due to numerical instability of |
3432 | doing bezier curve intersections. |
3433 | */ |
3434 | QPainterPath QPainterPath::intersected(const QPainterPath &p) const |
3435 | { |
3436 | if (isEmpty() || p.isEmpty()) |
3437 | return QPainterPath(); |
3438 | QPathClipper clipper(*this, p); |
3439 | return clipper.clip(op: QPathClipper::BoolAnd); |
3440 | } |
3441 | |
3442 | /*! |
3443 | \since 4.3 |
3444 | |
3445 | Returns a path which is \a p's fill area subtracted from this path's fill area. |
3446 | |
3447 | Set operations on paths will treat the paths as areas. Non-closed |
3448 | paths will be treated as implicitly closed. |
3449 | Bezier curves may be flattened to line segments due to numerical instability of |
3450 | doing bezier curve intersections. |
3451 | */ |
3452 | QPainterPath QPainterPath::subtracted(const QPainterPath &p) const |
3453 | { |
3454 | if (isEmpty() || p.isEmpty()) |
3455 | return *this; |
3456 | QPathClipper clipper(*this, p); |
3457 | return clipper.clip(op: QPathClipper::BoolSub); |
3458 | } |
3459 | |
3460 | #if QT_DEPRECATED_SINCE(5, 13) |
3461 | /*! |
3462 | \since 4.3 |
3463 | \obsolete |
3464 | |
3465 | Use subtracted() instead. |
3466 | |
3467 | \sa subtracted() |
3468 | */ |
3469 | QPainterPath QPainterPath::subtractedInverted(const QPainterPath &p) const |
3470 | { |
3471 | return p.subtracted(p: *this); |
3472 | } |
3473 | #endif |
3474 | |
3475 | /*! |
3476 | \since 4.4 |
3477 | |
3478 | Returns a simplified version of this path. This implies merging all subpaths that intersect, |
3479 | and returning a path containing no intersecting edges. Consecutive parallel lines will also |
3480 | be merged. The simplified path will always use the default fill rule, Qt::OddEvenFill. |
3481 | Bezier curves may be flattened to line segments due to numerical instability of |
3482 | doing bezier curve intersections. |
3483 | */ |
3484 | QPainterPath QPainterPath::simplified() const |
3485 | { |
3486 | if(isEmpty()) |
3487 | return *this; |
3488 | QPathClipper clipper(*this, QPainterPath()); |
3489 | return clipper.clip(op: QPathClipper::Simplify); |
3490 | } |
3491 | |
3492 | /*! |
3493 | \since 4.3 |
3494 | |
3495 | Returns \c true if the current path intersects at any point the given path \a p. |
3496 | Also returns \c true if the current path contains or is contained by any part of \a p. |
3497 | |
3498 | Set operations on paths will treat the paths as areas. Non-closed |
3499 | paths will be treated as implicitly closed. |
3500 | |
3501 | \sa contains() |
3502 | */ |
3503 | bool QPainterPath::intersects(const QPainterPath &p) const |
3504 | { |
3505 | if (p.elementCount() == 1) |
3506 | return contains(pt: p.elementAt(i: 0)); |
3507 | if (isEmpty() || p.isEmpty()) |
3508 | return false; |
3509 | QPathClipper clipper(*this, p); |
3510 | return clipper.intersect(); |
3511 | } |
3512 | |
3513 | /*! |
3514 | \since 4.3 |
3515 | |
3516 | Returns \c true if the given path \a p is contained within |
3517 | the current path. Returns \c false if any edges of the current path and |
3518 | \a p intersect. |
3519 | |
3520 | Set operations on paths will treat the paths as areas. Non-closed |
3521 | paths will be treated as implicitly closed. |
3522 | |
3523 | \sa intersects() |
3524 | */ |
3525 | bool QPainterPath::contains(const QPainterPath &p) const |
3526 | { |
3527 | if (p.elementCount() == 1) |
3528 | return contains(pt: p.elementAt(i: 0)); |
3529 | if (isEmpty() || p.isEmpty()) |
3530 | return false; |
3531 | QPathClipper clipper(*this, p); |
3532 | return clipper.contains(); |
3533 | } |
3534 | |
3535 | void QPainterPath::setDirty(bool dirty) |
3536 | { |
3537 | d_func()->dirtyBounds = dirty; |
3538 | d_func()->dirtyControlBounds = dirty; |
3539 | d_func()->pathConverter.reset(); |
3540 | d_func()->convex = false; |
3541 | } |
3542 | |
3543 | void QPainterPath::computeBoundingRect() const |
3544 | { |
3545 | QPainterPathData *d = d_func(); |
3546 | d->dirtyBounds = false; |
3547 | if (!d_ptr) { |
3548 | d->bounds = QRect(); |
3549 | return; |
3550 | } |
3551 | |
3552 | qreal minx, maxx, miny, maxy; |
3553 | minx = maxx = d->elements.at(i: 0).x; |
3554 | miny = maxy = d->elements.at(i: 0).y; |
3555 | for (int i=1; i<d->elements.size(); ++i) { |
3556 | const Element &e = d->elements.at(i); |
3557 | |
3558 | switch (e.type) { |
3559 | case MoveToElement: |
3560 | case LineToElement: |
3561 | if (e.x > maxx) maxx = e.x; |
3562 | else if (e.x < minx) minx = e.x; |
3563 | if (e.y > maxy) maxy = e.y; |
3564 | else if (e.y < miny) miny = e.y; |
3565 | break; |
3566 | case CurveToElement: |
3567 | { |
3568 | QBezier b = QBezier::fromPoints(p1: d->elements.at(i: i-1), |
3569 | p2: e, |
3570 | p3: d->elements.at(i: i+1), |
3571 | p4: d->elements.at(i: i+2)); |
3572 | QRectF r = qt_painterpath_bezier_extrema(b); |
3573 | qreal right = r.right(); |
3574 | qreal bottom = r.bottom(); |
3575 | if (r.x() < minx) minx = r.x(); |
3576 | if (right > maxx) maxx = right; |
3577 | if (r.y() < miny) miny = r.y(); |
3578 | if (bottom > maxy) maxy = bottom; |
3579 | i += 2; |
3580 | } |
3581 | break; |
3582 | default: |
3583 | break; |
3584 | } |
3585 | } |
3586 | d->bounds = QRectF(minx, miny, maxx - minx, maxy - miny); |
3587 | } |
3588 | |
3589 | |
3590 | void QPainterPath::computeControlPointRect() const |
3591 | { |
3592 | QPainterPathData *d = d_func(); |
3593 | d->dirtyControlBounds = false; |
3594 | if (!d_ptr) { |
3595 | d->controlBounds = QRect(); |
3596 | return; |
3597 | } |
3598 | |
3599 | qreal minx, maxx, miny, maxy; |
3600 | minx = maxx = d->elements.at(i: 0).x; |
3601 | miny = maxy = d->elements.at(i: 0).y; |
3602 | for (int i=1; i<d->elements.size(); ++i) { |
3603 | const Element &e = d->elements.at(i); |
3604 | if (e.x > maxx) maxx = e.x; |
3605 | else if (e.x < minx) minx = e.x; |
3606 | if (e.y > maxy) maxy = e.y; |
3607 | else if (e.y < miny) miny = e.y; |
3608 | } |
3609 | d->controlBounds = QRectF(minx, miny, maxx - minx, maxy - miny); |
3610 | } |
3611 | |
3612 | #ifndef QT_NO_DEBUG_STREAM |
3613 | QDebug operator<<(QDebug s, const QPainterPath &p) |
3614 | { |
3615 | s.nospace() << "QPainterPath: Element count=" << p.elementCount() << Qt::endl; |
3616 | const char *types[] = {"MoveTo" , "LineTo" , "CurveTo" , "CurveToData" }; |
3617 | for (int i=0; i<p.elementCount(); ++i) { |
3618 | s.nospace() << " -> " << types[p.elementAt(i).type] << "(x=" << p.elementAt(i).x << ", y=" << p.elementAt(i).y << ')' << Qt::endl; |
3619 | |
3620 | } |
3621 | return s; |
3622 | } |
3623 | #endif |
3624 | |
3625 | QT_END_NAMESPACE |
3626 | |