14#include <qtextlayout.h>
15#include <qvarlengtharray.h>
18#include <private/qbezier_p.h>
19#include <private/qfontengine_p.h>
20#include <private/qnumeric_p.h>
21#include <private/qobject_p.h>
22#include <private/qpathclipper_p.h>
23#include <private/qstroker_p.h>
24#include <private/qtextengine_p.h>
31#include <performance.h>
42 if (
sizeof(qreal) >=
sizeof(
double))
43 return qIsFinite(c) && fabs(c) < 1e128;
45 return qIsFinite(c) && fabsf(
float(c)) < 1e16f;
50 return isValidCoord(p.x()) && isValidCoord(p.y());
55 return isValidCoord(r.x()) && isValidCoord(r.y()) && isValidCoord(r.width()) && isValidCoord(r.height());
69 QPointF* startPoint, QPointF *endPoint)
73 *startPoint = QPointF();
75 *endPoint = QPointF();
79 qreal w2 = r.width() / 2;
80 qreal h2 = r.height() / 2;
82 qreal angles[2] = { angle, angle + length };
83 QPointF *points[2] = { startPoint, endPoint };
85 for (
int i = 0; i < 2; ++i) {
89 qreal theta = angles[i] - 360 *
std::floor(angles[i] / 360);
92 int quadrant =
int(t);
95 t = qt_t_for_arc_angle(90 * t);
102 QBezier::coefficients(t, a, b, c, d);
103 QPointF p(a + b + c*QT_PATH_KAPPA, d + c + b*QT_PATH_KAPPA);
106 if (quadrant == 1 || quadrant == 2)
110 if (quadrant == 0 || quadrant == 1)
113 *points[i] = r.center() + QPointF(w2 * p.x(), h2 * p.y());
118static void qt_debug_path(
const QPainterPath &path)
120 const char *names[] = {
127 printf(
"\nQPainterPath: elementCount=%d\n", path.elementCount());
128 for (
int i=0; i<path.elementCount(); ++i) {
129 const QPainterPath::Element &e = path.elementAt(i);
130 Q_ASSERT(e.type >= 0 && e.type <= QPainterPath::CurveToDataElement);
131 printf(
" - %3d:: %s, (%.2f, %.2f)\n", i, names[e.type], e.x, e.y);
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
374
375
376
377
378
381
382
383
384
385
388
389
390
391
392
395
396
397
398
399
400
401
402
405
406
407
408
409
410
411
412
415
416
417
418
419
420
423
424
425
426
427
428
431
432
433
434
435
436
437
440
441
442
443
444
445
448
449
450
451
452
453
454
457
458
459
460
461
462
463
464
467
468
469
470
471
472
473
476
477
478
479
480
481
482
483
484
485
486auto QPainterPath::elements()
const noexcept -> QSpan<
const Element>
490 return d_ptr->elements;
494
495
496
497
498
499
500
502int QPainterPath::elementCount()
const
504 return d_ptr ? d_ptr->elements.size() : 0;
508
509
510
511
512
513
514
516QPainterPath::Element QPainterPath::elementAt(
int i)
const
519 Q_ASSERT(i >= 0 && i < elementCount());
520 return d_ptr->elements.at(i);
524
525
526
527
528
529
531void QPainterPath::setElementPositionAt(
int i, qreal x, qreal y)
534 Q_ASSERT(i >= 0 && i < elementCount());
536 QPainterPath::Element &e = d_ptr->elements[i];
543
544
545
546
547
550
551
552QPainterPath::QPainterPath()
noexcept
558
559
560
561
562
563
564QPainterPath::QPainterPath(
const QPainterPath &other)
565 : d_ptr(other.d_ptr ?
new QPainterPathPrivate(*other.d_ptr) :
nullptr)
570
571
572
573
574
575
576
579
580
581
583QPainterPath::QPainterPath(
const QPointF &startPoint)
584 : d_ptr(
new QPainterPathPrivate(startPoint))
589
590
591void QPainterPath::ensureData_helper()
593 Q_ASSERT(d_ptr ==
nullptr);
594 QPainterPathPrivate *data =
new QPainterPathPrivate;
595 data->elements.reserve(16);
596 QPainterPath::Element e = { 0, 0, QPainterPath::MoveToElement };
599 Q_ASSERT(d_ptr !=
nullptr);
603
604
605
606
607
608
609QPainterPath &QPainterPath::operator=(
const QPainterPath &other)
611 QPainterPath copy(other);
617
618
619
620
621
622
625
626
627
628
631
632
633QPainterPath::~QPainterPath()
639
640
641
642
643
644
645
646void QPainterPath::clear()
653 d_func()->elements.append( {0, 0, MoveToElement} );
657
658
659
660
661
662
663
664void QPainterPath::reserve(
int size)
667 if ((!d && size > 0) || (d && d->elements.capacity() < size)) {
670 d_func()->elements.reserve(size);
675
676
677
678
679
680int QPainterPath::capacity()
const
684 return d->elements.capacity();
690
691
692
693
694
695
696
697
698
699
700void QPainterPath::closeSubpath()
703 printf(
"QPainterPath::closeSubpath()\n");
713
714
715
716
717
718
719
722
723
724
725
726
727
728
729
730void QPainterPath::moveTo(
const QPointF &p)
733 printf(
"QPainterPath::moveTo() (%.2f,%.2f)\n", p.x(), p.y());
736 if (!hasValidCoords(p)) {
738 qWarning(
"QPainterPath::moveTo: Adding point with invalid coordinates, ignoring call");
746 QPainterPathPrivate *d = d_func();
747 Q_ASSERT(!d->elements.isEmpty());
749 d->require_moveTo =
false;
751 if (d->elements.constLast().type == MoveToElement) {
752 d->elements.last().x = p.x();
753 d->elements.last().y = p.y();
755 Element elm = { p.x(), p.y(), MoveToElement };
756 d->elements.append(elm);
758 d->cStart = d->elements.size() - 1;
762
763
764
765
766
767
768
771
772
773
774
775
776
777
778
779
780void QPainterPath::lineTo(
const QPointF &p)
783 printf(
"QPainterPath::lineTo() (%.2f,%.2f)\n", p.x(), p.y());
786 if (!hasValidCoords(p)) {
788 qWarning(
"QPainterPath::lineTo: Adding point with invalid coordinates, ignoring call");
796 QPainterPathPrivate *d = d_func();
797 Q_ASSERT(!d->elements.isEmpty());
799 if (p == QPointF(d->elements.constLast()))
801 Element elm = { p.x(), p.y(), LineToElement };
802 d->elements.append(elm);
804 d->convex = d->elements.size() == 3 || (d->elements.size() == 4 && d->isClosed());
808
809
810
811
812
813
814
815
816
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839void QPainterPath::cubicTo(
const QPointF &c1,
const QPointF &c2,
const QPointF &e)
842 printf(
"QPainterPath::cubicTo() (%.2f,%.2f), (%.2f,%.2f), (%.2f,%.2f)\n",
843 c1.x(), c1.y(), c2.x(), c2.y(), e.x(), e.y());
846 if (!hasValidCoords(c1) || !hasValidCoords(c2) || !hasValidCoords(e)) {
848 qWarning(
"QPainterPath::cubicTo: Adding point with invalid coordinates, ignoring call");
856 QPainterPathPrivate *d = d_func();
857 Q_ASSERT(!d->elements.isEmpty());
862 if (d->elements.constLast() == c1 && c1 == c2 && c2 == e)
867 Element ce1 = { c1.x(), c1.y(), CurveToElement };
868 Element ce2 = { c2.x(), c2.y(), CurveToDataElement };
869 Element ee = { e.x(), e.y(), CurveToDataElement };
870 d->elements << ce1 << ce2 << ee;
874
875
876
877
878
879
880
881
884
885
886
887
888
889
890
891
892
893
894
895void QPainterPath::quadTo(
const QPointF &c,
const QPointF &e)
898 printf(
"QPainterPath::quadTo() (%.2f,%.2f), (%.2f,%.2f)\n",
899 c.x(), c.y(), e.x(), e.y());
902 if (!hasValidCoords(c) || !hasValidCoords(e)) {
904 qWarning(
"QPainterPath::quadTo: Adding point with invalid coordinates, ignoring call");
913 Q_ASSERT(!d->elements.isEmpty());
914 const QPainterPath::Element &elm = d->elements.at(elementCount()-1);
915 QPointF prev(elm.x, elm.y);
919 if (prev == c && c == e)
922 QPointF c1((prev.x() + 2*c.x()) / 3, (prev.y() + 2*c.y()) / 3);
923 QPointF c2((e.x() + 2*c.x()) / 3, (e.y() + 2*c.y()) / 3);
928
929
930
931
932
933
934
935
936
937
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970void QPainterPath::arcTo(
const QRectF &rect, qreal startAngle, qreal sweepLength)
973 printf(
"QPainterPath::arcTo() (%.2f, %.2f, %.2f, %.2f, angle=%.2f, sweep=%.2f\n",
974 rect.x(), rect.y(), rect.width(), rect.height(), startAngle, sweepLength);
977 if (!hasValidCoords(rect) || !isValidCoord(startAngle) || !isValidCoord(sweepLength)) {
979 qWarning(
"QPainterPath::arcTo: Adding point with invalid coordinates, ignoring call");
992 QPointF curve_start = qt_curves_for_arc(rect, startAngle, sweepLength, pts, &point_count);
995 for (
int i=0; i<point_count; i+=3) {
996 cubicTo(pts[i].x(), pts[i].y(),
997 pts[i+1].x(), pts[i+1].y(),
998 pts[i+2].x(), pts[i+2].y());
1005
1006
1007
1008
1009
1010
1011
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1031void QPainterPath::arcMoveTo(
const QRectF &rect, qreal angle)
1033 if (!hasValidCoords(rect) || !isValidCoord(angle)) {
1035 qWarning(
"QPainterPath::arcMoveTo: Adding point with invalid coordinates, ignoring call");
1044 qt_find_ellipse_coords(rect, angle, 0, &pt,
nullptr);
1051
1052
1053
1054
1055QPointF QPainterPath::currentPosition()
const
1057 return !d_ptr || d_func()->elements.isEmpty()
1059 : QPointF(d_func()->elements.constLast().x, d_func()->elements.constLast().y);
1064
1065
1066
1067
1068
1069
1070
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092void QPainterPath::addRect(
const QRectF &r)
1094 if (!hasValidCoords(r)) {
1096 qWarning(
"QPainterPath::addRect: Adding point with invalid coordinates, ignoring call");
1107 bool first = d_func()->elements.size() < 2;
1109 moveTo(r.x(), r.y());
1111 Element l1 = { r.x() + r.width(), r.y(), LineToElement };
1112 Element l2 = { r.x() + r.width(), r.y() + r.height(), LineToElement };
1113 Element l3 = { r.x(), r.y() + r.height(), LineToElement };
1114 Element l4 = { r.x(), r.y(), LineToElement };
1116 d_func()->elements << l1 << l2 << l3 << l4;
1117 d_func()->require_moveTo =
true;
1118 d_func()->convex = first;
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139void QPainterPath::addPolygon(
const QPolygonF &polygon)
1141 if (polygon.isEmpty())
1147 moveTo(polygon.constFirst());
1148 for (
int i=1; i<polygon.size(); ++i) {
1149 Element elm = { polygon.at(i).x(), polygon.at(i).y(), LineToElement };
1150 d_func()->elements << elm;
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177void QPainterPath::addEllipse(
const QRectF &boundingRect)
1179 if (!hasValidCoords(boundingRect)) {
1181 qWarning(
"QPainterPath::addEllipse: Adding point with invalid coordinates, ignoring call");
1186 if (boundingRect.isNull())
1192 bool first = d_func()->elements.size() < 2;
1196 QPointF start = qt_curves_for_arc(boundingRect, 0, -360, pts, &point_count);
1199 cubicTo(pts[0], pts[1], pts[2]);
1200 cubicTo(pts[3], pts[4], pts[5]);
1201 cubicTo(pts[6], pts[7], pts[8]);
1202 cubicTo(pts[9], pts[10], pts[11]);
1203 d_func()->require_moveTo =
true;
1205 d_func()->convex = first;
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229void QPainterPath::addText(
const QPointF &point,
const QFont &f,
const QString &text)
1237 QTextLayout layout(text, f);
1238 layout.setCacheEnabled(
true);
1240 QTextOption opt = layout.textOption();
1241 opt.setUseDesignMetrics(
true);
1242 layout.setTextOption(opt);
1244 QTextEngine *eng = layout.engine();
1245 layout.beginLayout();
1246 QTextLine line = layout.createLine();
1249 const QScriptLine &sl = eng->lines[0];
1250 if (!sl.length || !eng->layoutData)
1253 int nItems = eng->layoutData->items.size();
1258 QVarLengthArray<
int> visualOrder(nItems);
1259 QVarLengthArray<uchar> levels(nItems);
1260 for (
int i = 0; i < nItems; ++i)
1261 levels[i] = eng->layoutData->items.at(i).analysis.bidiLevel;
1262 QTextEngine::bidiReorder(nItems, levels.data(), visualOrder.data());
1264 for (
int i = 0; i < nItems; ++i) {
1265 int item = visualOrder[i];
1266 const QScriptItem &si = eng->layoutData->items.at(item);
1268 if (si.analysis.flags < QScriptAnalysis::TabOrObject) {
1269 QGlyphLayout glyphs = eng->shapedGlyphs(&si);
1270 QFontEngine *fe = eng->fontEngine(si);
1272 fe->addOutlineToPath(x, y, glyphs,
this,
1273 si.analysis.bidiLevel % 2
1274 ? QTextItem::RenderFlags(QTextItem::RightToLeft)
1275 : QTextItem::RenderFlags{});
1277 const qreal lw = fe->lineThickness().toReal();
1278 if (f.d->underline) {
1279 qreal pos = fe->underlinePosition().toReal();
1280 addRect(x, y + pos, si.width.toReal(), lw);
1282 if (f.d->overline) {
1283 qreal pos = fe->ascent().toReal() + 1;
1284 addRect(x, y - pos, si.width.toReal(), lw);
1286 if (f.d->strikeOut) {
1287 qreal pos = fe->ascent().toReal() / 3;
1288 addRect(x, y - pos, si.width.toReal(), lw);
1291 x += si.width.toReal();
1296
1297
1298
1299
1300
1301
1302
1303void QPainterPath::addPath(
const QPainterPath &other)
1305 if (other.isEmpty())
1311 QPainterPathPrivate *d = d_func();
1313 if (d->elements.constLast().type == MoveToElement)
1314 d->elements.remove(d->elements.size()-1);
1317 int cStart = d->elements.size() + other.d_func()->cStart;
1318 d->elements += other.d_func()->elements;
1321 d->require_moveTo = other.d_func()->isClosed();
1326
1327
1328
1329
1330
1331
1332
1333
1334void QPainterPath::connectPath(
const QPainterPath &other)
1336 if (other.isEmpty())
1342 QPainterPathPrivate *d = d_func();
1344 if (d->elements.constLast().type == MoveToElement)
1345 d->elements.remove(d->elements.size()-1);
1348 int cStart = d->elements.size() + other.d_func()->cStart;
1349 int first = d->elements.size();
1350 d->elements += other.d_func()->elements;
1353 d->elements[first].type = LineToElement;
1356 if (first > 0 && QPointF(d->elements.at(first)) == QPointF(d->elements.at(first - 1))) {
1357 d->elements.remove(first--);
1361 if (cStart != first)
1366
1367
1368
1369
1370
1371
1372void QPainterPath::addRegion(
const QRegion ®ion)
1377 for (
const QRect &rect : region)
1383
1384
1385
1386
1387Qt::FillRule QPainterPath::fillRule()
const
1389 return d_func() && d_func()->hasWindingFill ? Qt::WindingFill : Qt::OddEvenFill;
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409void QPainterPath::setFillRule(Qt::FillRule fillRule)
1412 const bool isWindingRequested = (fillRule == Qt::WindingFill);
1413 if (d_func()->hasWindingFill == isWindingRequested)
1417 d_func()->hasWindingFill = isWindingRequested;
1420#define QT_BEZIER_A(bezier, coord) 3
* (-bezier.coord##1
1425#define QT_BEZIER_B(bezier, coord) 6
* (bezier.coord##1
1429#define QT_BEZIER_C(bezier, coord) 3
* (- bezier.coord##1
1432#define QT_BEZIER_CHECK_T(bezier, t)
1433 if (t >= 0
&& t <= 1
) {
1434 QPointF p(b.pointAt(t));
1435 if (p.x() < minx) minx = p.x();
1436 else if (p.x() > maxx) maxx = p.x();
1437 if (p.y() < miny) miny = p.y();
1438 else if (p.y() > maxy) maxy = p.y();
1444 qreal minx, miny, maxx, maxy;
1468 if (qFuzzyIsNull(ax)) {
1471 if (!qFuzzyIsNull(bx)) {
1477 const qreal tx = bx * bx - 4 * ax * cx;
1480 qreal temp = qSqrt(tx);
1481 qreal rcp = 1 / (2 * ax);
1482 qreal t1 = (-bx + temp) * rcp;
1485 qreal t2 = (-bx - temp) * rcp;
1498 if (qFuzzyIsNull(ay)) {
1501 if (!qFuzzyIsNull(by)) {
1507 const qreal ty = by * by - 4 * ay * cy;
1510 qreal temp = qSqrt(ty);
1511 qreal rcp = 1 / (2 * ay);
1512 qreal t1 = (-by + temp) * rcp;
1515 qreal t2 = (-by - temp) * rcp;
1520 return QRectF(minx, miny, maxx - minx, maxy - miny);
1524
1525
1526
1527
1528
1529QRectF QPainterPath::boundingRect()
const
1533 QPainterPathPrivate *d = d_func();
1536 computeBoundingRect();
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550QRectF QPainterPath::controlPointRect()
const
1554 QPainterPathPrivate *d = d_func();
1556 if (d->dirtyControlBounds)
1557 computeControlPointRect();
1558 return d->controlBounds;
1563
1564
1565
1566
1567
1568
1569
1571bool QPainterPath::isEmpty()
const
1573 return !d_ptr || (d_ptr->elements.size() == 1 && d_ptr->elements.constFirst().type == MoveToElement);
1577
1578
1579
1580
1581
1582
1583
1584QPainterPath QPainterPath::toReversed()
const
1586 Q_D(
const QPainterPath);
1594 rev.moveTo(d->elements.at(d->elements.size()-1).x, d->elements.at(d->elements.size()-1).y);
1596 for (
int i=d->elements.size()-1; i>=1; --i) {
1597 const QPainterPath::Element &elm = d->elements.at(i);
1598 const QPainterPath::Element &prev = d->elements.at(i-1);
1601 rev.lineTo(prev.x, prev.y);
1604 rev.moveTo(prev.x, prev.y);
1606 case CurveToDataElement:
1609 const QPainterPath::Element &cp1 = d->elements.at(i-2);
1610 const QPainterPath::Element &sp = d->elements.at(i-3);
1611 Q_ASSERT(prev.type == CurveToDataElement);
1612 Q_ASSERT(cp1.type == CurveToElement);
1613 rev.cubicTo(prev.x, prev.y, cp1.x, cp1.y, sp.x, sp.y);
1618 Q_ASSERT(!
"qt_reversed_path");
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638QList<QPolygonF> QPainterPath::toSubpathPolygons(
const QTransform &matrix)
const
1641 Q_D(
const QPainterPath);
1642 QList<QPolygonF> flatCurves;
1647 for (
int i=0; i<elementCount(); ++i) {
1648 const QPainterPath::Element &e = d->elements.at(i);
1650 case QPainterPath::MoveToElement:
1651 if (current.size() > 1)
1652 flatCurves += current;
1654 current.reserve(16);
1655 current += QPointF(e.x, e.y) * matrix;
1657 case QPainterPath::LineToElement:
1658 current += QPointF(e.x, e.y) * matrix;
1660 case QPainterPath::CurveToElement: {
1661 Q_ASSERT(d->elements.at(i+1).type == QPainterPath::CurveToDataElement);
1662 Q_ASSERT(d->elements.at(i+2).type == QPainterPath::CurveToDataElement);
1663 QBezier bezier = QBezier::fromPoints(QPointF(d->elements.at(i-1).x, d->elements.at(i-1).y) * matrix,
1664 QPointF(e.x, e.y) * matrix,
1665 QPointF(d->elements.at(i+1).x, d->elements.at(i+1).y) * matrix,
1666 QPointF(d->elements.at(i+2).x, d->elements.at(i+2).y) * matrix);
1667 bezier.addToPolygon(¤t);
1671 case QPainterPath::CurveToDataElement:
1672 Q_ASSERT(!
"QPainterPath::toSubpathPolygons(), bad element type");
1677 if (current.size()>1)
1678 flatCurves += current;
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705QList<QPolygonF> QPainterPath::toFillPolygons(
const QTransform &matrix)
const
1708 QList<QPolygonF> polys;
1710 QList<QPolygonF> subpaths = toSubpathPolygons(matrix);
1711 int count = subpaths.size();
1716 QList<QRectF> bounds;
1717 bounds.reserve(count);
1718 for (
int i=0; i<count; ++i)
1719 bounds += subpaths.at(i).boundingRect();
1721#ifdef QPP_FILLPOLYGONS_DEBUG
1722 printf(
"QPainterPath::toFillPolygons, subpathCount=%d\n", count);
1723 for (
int i=0; i<bounds.size(); ++i)
1724 qDebug() <<
" bounds" << i << bounds.at(i);
1727 QList< QList<
int> > isects;
1728 isects.resize(count);
1731 for (
int j=0; j<count; ++j) {
1732 if (subpaths.at(j).size() <= 2)
1734 QRectF cbounds = bounds.at(j);
1735 for (
int i=0; i<count; ++i) {
1736 if (cbounds.intersects(bounds.at(i))) {
1742#ifdef QPP_FILLPOLYGONS_DEBUG
1743 printf(
"Intersections before flattening:\n");
1744 for (
int i = 0; i < count; ++i) {
1746 for (
int j = 0; j < isects[i].size(); ++j) {
1747 printf(
"%d ", isects[i][j]);
1754 for (
int i=0; i<count; ++i) {
1755 const QList<
int> ¤t_isects = isects.at(i);
1756 for (
int j=0; j<current_isects.size(); ++j) {
1757 int isect_j = current_isects.at(j);
1760 const QList<
int> &isects_j = isects.at(isect_j);
1761 for (
int k = 0, size = isects_j.size(); k < size; ++k) {
1762 int isect_k = isects_j.at(k);
1763 if (isect_k != i && !isects.at(i).contains(isect_k)) {
1764 isects[i] += isect_k;
1767 isects[isect_j].clear();
1771#ifdef QPP_FILLPOLYGONS_DEBUG
1772 printf(
"Intersections after flattening:\n");
1773 for (
int i = 0; i < count; ++i) {
1775 for (
int j = 0; j < isects[i].size(); ++j) {
1776 printf(
"%d ", isects[i][j]);
1783 for (
int i=0; i<count; ++i) {
1784 const QList<
int> &subpath_list = isects.at(i);
1785 if (!subpath_list.isEmpty()) {
1787 for (
int j=0; j<subpath_list.size(); ++j) {
1788 const QPolygonF &subpath = subpaths.at(subpath_list.at(j));
1790 if (!subpath.isClosed())
1791 buildUp += subpath.first();
1792 if (!buildUp.isClosed())
1793 buildUp += buildUp.constFirst();
1816 if (QtPrivate::fuzzyCompare(y1, y2)) {
1819 }
else if (y2 < y1) {
1820 qreal x_tmp = x2; x2 = x1; x1 = x_tmp;
1821 qreal y_tmp = y2; y2 = y1; y1 = y_tmp;
1825 if (y >= y1 && y < y2) {
1826 qreal x = x1 + ((x2 - x1) / (y2 - y1)) * (y - y1);
1836 int *winding,
int depth = 0)
1840 QRectF bounds = bezier.bounds();
1846 if (y >= bounds.y() && y < bounds.y() + bounds.height()) {
1850 const qreal lower_bound = qreal(.001);
1851 if (depth == 32 || (bounds.width() < lower_bound && bounds.height() < lower_bound)) {
1855 if (bezier.pt1().x() <= x) {
1856 (*winding) += (bezier.pt4().y() > bezier.pt1().y() ? 1 : -1);
1862 const auto halves = bezier.split();
1863 qt_painterpath_isect_curve(halves.first, pt, winding, depth + 1);
1864 qt_painterpath_isect_curve(halves.second, pt, winding, depth + 1);
1869
1870
1871
1872
1873
1874
1875
1876bool QPainterPath::contains(
const QPointF &pt)
const
1878 if (isEmpty() || !controlPointRect().contains(pt))
1881 QPainterPathPrivate *d = d_func();
1883 int winding_number = 0;
1887 for (
int i=0; i<d->elements.size(); ++i) {
1888 const Element &e = d->elements.at(i);
1894 qt_painterpath_isect_line(last_pt, last_start, pt, &winding_number);
1895 last_start = last_pt = e;
1899 qt_painterpath_isect_line(last_pt, e, pt, &winding_number);
1903 case CurveToElement:
1905 const QPainterPath::Element &cp2 = d->elements.at(++i);
1906 const QPainterPath::Element &ep = d->elements.at(++i);
1907 qt_painterpath_isect_curve(QBezier::fromPoints(last_pt, e, cp2, ep),
1908 pt, &winding_number);
1920 if (last_pt != last_start)
1921 qt_painterpath_isect_line(last_pt, last_start, pt, &winding_number);
1923 return (d->hasWindingFill
1924 ? (winding_number != 0)
1925 : ((winding_number % 2) != 0));
1933 qreal left = rect.left();
1934 qreal right = rect.right();
1935 qreal top = rect.top();
1936 qreal bottom = rect.bottom();
1939 int p1 = ((x1 < left) <<
Left)
1940 | ((x1 > right) <<
Right)
1941 | ((y1 < top) <<
Top)
1942 | ((y1 > bottom) <<
Bottom);
1943 int p2 = ((x2 < left) <<
Left)
1944 | ((x2 > right) <<
Right)
1945 | ((y2 < top) <<
Top)
1946 | ((y2 > bottom) <<
Bottom);
1958 y1 += dy/dx * (left - x1);
1960 }
else if (x1 > right) {
1961 y1 -= dy/dx * (x1 - right);
1965 y2 += dy/dx * (left - x2);
1967 }
else if (x2 > right) {
1968 y2 -= dy/dx * (x2 - right);
1972 p1 = ((y1 < top) <<
Top)
1973 | ((y1 > bottom) <<
Bottom);
1974 p2 = ((y2 < top) <<
Top)
1975 | ((y2 > bottom) <<
Bottom);
1982 x1 += dx/dy * (top - y1);
1984 }
else if (y1 > bottom) {
1985 x1 -= dx/dy * (y1 - bottom);
1989 x2 += dx/dy * (top - y2);
1991 }
else if (y2 > bottom) {
1992 x2 -= dx/dy * (y2 - bottom);
1996 p1 = ((x1 < left) <<
Left)
1997 | ((x1 > right) <<
Right);
1998 p2 = ((x2 < left) <<
Left)
1999 | ((x2 > right) <<
Right);
2011 QRectF bounds = bezier.bounds();
2013 if (y >= bounds.top() && y < bounds.bottom()
2014 && bounds.right() >= x1 && bounds.left() < x2) {
2015 const qreal lower_bound = qreal(.01);
2016 if (depth == 32 || (bounds.width() < lower_bound && bounds.height() < lower_bound))
2019 const auto halves = bezier.split();
2020 if (qt_isect_curve_horizontal(halves.first, y, x1, x2, depth + 1)
2021 || qt_isect_curve_horizontal(halves.second, y, x1, x2, depth + 1))
2029 QRectF bounds = bezier.bounds();
2031 if (x >= bounds.left() && x < bounds.right()
2032 && bounds.bottom() >= y1 && bounds.top() < y2) {
2033 const qreal lower_bound = qreal(.01);
2034 if (depth == 32 || (bounds.width() < lower_bound && bounds.height() < lower_bound))
2037 const auto halves = bezier.split();
2038 if (qt_isect_curve_vertical(halves.first, x, y1, y2, depth + 1)
2039 || qt_isect_curve_vertical(halves.second, x, y1, y2, depth + 1))
2047 if ((point.x() == rect.left() || point.x() == rect.right()) &&
2048 (point.y() >= rect.top() && point.y() <= rect.bottom()))
2050 if ((point.y() == rect.top() || point.y() == rect.bottom()) &&
2051 (point.x() >= rect.left() && point.x() <= rect.right()))
2057
2058
2063 enum { OnRect, InsideRect, OutsideRect} edgeStatus = OnRect;
2064 for (
int i=0; i<path->elementCount(); ++i) {
2065 const QPainterPath::Element &e = path->elementAt(i);
2069 case QPainterPath::MoveToElement:
2071 && qFuzzyCompare(last_pt, last_start)
2072 && qt_painterpath_isect_line_rect(last_pt.x(), last_pt.y(),
2073 last_start.x(), last_start.y(), rect))
2075 last_start = last_pt = e;
2078 case QPainterPath::LineToElement:
2079 if (qt_painterpath_isect_line_rect(last_pt.x(), last_pt.y(), e.x, e.y, rect))
2084 case QPainterPath::CurveToElement:
2086 QPointF cp2 = path->elementAt(++i);
2087 QPointF ep = path->elementAt(++i);
2088 QBezier bezier = QBezier::fromPoints(last_pt, e, cp2, ep);
2089 if (qt_isect_curve_horizontal(bezier, rect.top(), rect.left(), rect.right())
2090 || qt_isect_curve_horizontal(bezier, rect.bottom(), rect.left(), rect.right())
2091 || qt_isect_curve_vertical(bezier, rect.left(), rect.top(), rect.bottom())
2092 || qt_isect_curve_vertical(bezier, rect.right(), rect.top(), rect.bottom()))
2103 if (!pointOnEdge(rect, last_pt)) {
2104 bool contained = rect.contains(last_pt);
2105 switch (edgeStatus) {
2115 edgeStatus = contained ? InsideRect : OutsideRect;
2119 if (last_pt == last_start)
2120 edgeStatus = OnRect;
2125 if (last_pt != last_start
2126 && qt_painterpath_isect_line_rect(last_pt.x(), last_pt.y(),
2127 last_start.x(), last_start.y(), rect))
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147bool QPainterPath::intersects(
const QRectF &rect)
const
2149 if (elementCount() == 1 && rect.contains(elementAt(0)))
2155 QRectF cp = controlPointRect();
2156 QRectF rn = rect.normalized();
2161 if (qMax(rn.left(), cp.left()) > qMin(rn.right(), cp.right())
2162 || qMax(rn.top(), cp.top()) > qMin(rn.bottom(), cp.bottom()))
2166 if (qt_painterpath_check_crossing(
this, rect))
2169 if (contains(rect.center()))
2175 for (
int i=0; i<d->elements.size(); ++i) {
2176 const Element &e = d->elements.at(i);
2177 if (e.type == QPainterPath::MoveToElement && rect.contains(e))
2185
2186
2187
2188
2189
2190void QPainterPath::translate(qreal dx, qreal dy)
2192 if (!d_ptr || (dx == 0 && dy == 0))
2195 int elementsLeft = d_ptr->elements.size();
2196 if (elementsLeft <= 0)
2200 QPainterPath::Element *element = d_func()->elements.data();
2202 while (elementsLeft--) {
2210
2211
2212
2213
2214
2215
2216
2217
2220
2221
2222
2223
2224
2225QPainterPath QPainterPath::translated(qreal dx, qreal dy)
const
2227 QPainterPath copy(*
this);
2228 copy.translate(dx, dy);
2233
2234
2235
2236
2237
2238
2239
2240
2243
2244
2245
2246
2247
2248bool QPainterPath::contains(
const QRectF &rect)
const
2254 if (isEmpty() || !controlPointRect().contains(rect))
2260 if (qt_painterpath_check_crossing(
this, rect)) {
2261 if (fillRule() == Qt::OddEvenFill) {
2266 if (!contains(rect.topLeft()) ||
2267 !contains(rect.topRight()) ||
2268 !contains(rect.bottomRight()) ||
2269 !contains(rect.bottomLeft()))
2278 if (!contains(rect.center()))
2287 for (
int i=0; i<d->elements.size(); ++i) {
2288 const Element &e = d->elements.at(i);
2289 if (e.type == QPainterPath::MoveToElement && rect.contains(e)) {
2290 if (fillRule() == Qt::OddEvenFill)
2294 for (; !stop && i<d->elements.size(); ++i) {
2295 const Element &el = d->elements.at(i);
2304 case CurveToElement:
2305 if (!contains(d->elements.at(i+2)))
2322static inline bool epsilonCompare(
const QPointF &a,
const QPointF &b,
const QSizeF &epsilon)
2324 return qAbs(a.x() - b.x()) <= epsilon.width()
2325 && qAbs(a.y() - b.y()) <= epsilon.height();
2329
2330
2331
2332
2333
2334
2335
2337bool QPainterPath::operator==(
const QPainterPath &path)
const
2339 QPainterPathPrivate *d = d_func();
2340 QPainterPathPrivate *other_d = path.d_func();
2343 }
else if (!d || !other_d) {
2344 if (!other_d && isEmpty() && elementAt(0) == QPointF() && !d->hasWindingFill)
2346 if (!d && path.isEmpty() && path.elementAt(0) == QPointF() && !other_d->hasWindingFill)
2350 else if (d->hasWindingFill != other_d->hasWindingFill)
2352 else if (d->elements.size() != other_d->elements.size())
2355 const qreal qt_epsilon =
sizeof(qreal) ==
sizeof(
double) ? 1e-12 : qreal(1e-5);
2357 QSizeF epsilon = boundingRect().size();
2358 epsilon.rwidth() *= qt_epsilon;
2359 epsilon.rheight() *= qt_epsilon;
2361 for (
int i = 0; i < d->elements.size(); ++i)
2362 if (d->elements.at(i).type != other_d->elements.at(i).type
2363 || !epsilonCompare(d->elements.at(i), other_d->elements.at(i), epsilon))
2370
2371
2372
2373
2374
2375
2376
2378bool QPainterPath::operator!=(
const QPainterPath &path)
const
2380 return !(*
this==path);
2384
2385
2386
2387
2388
2389
2390QPainterPath QPainterPath::operator&(
const QPainterPath &other)
const
2392 return intersected(other);
2396
2397
2398
2399
2400
2401
2402QPainterPath QPainterPath::operator|(
const QPainterPath &other)
const
2404 return united(other);
2408
2409
2410
2411
2412
2413
2414
2415QPainterPath QPainterPath::operator+(
const QPainterPath &other)
const
2417 return united(other);
2421
2422
2423
2424
2425
2426
2427QPainterPath QPainterPath::operator-(
const QPainterPath &other)
const
2429 return subtracted(other);
2433
2434
2435
2436
2437
2438
2439QPainterPath &QPainterPath::operator&=(
const QPainterPath &other)
2441 return *
this = (*
this & other);
2445
2446
2447
2448
2449
2450
2451QPainterPath &QPainterPath::operator|=(
const QPainterPath &other)
2453 return *
this = (*
this | other);
2457
2458
2459
2460
2461
2462
2463
2464QPainterPath &QPainterPath::operator+=(
const QPainterPath &other)
2466 return *
this = (*
this + other);
2470
2471
2472
2473
2474
2475
2476
2477QPainterPath &QPainterPath::operator-=(
const QPainterPath &other)
2479 return *
this = (*
this - other);
2482#ifndef QT_NO_DATASTREAM
2484
2485
2486
2487
2488
2489
2490
2491
2499 s << p.elementCount();
2500 for (
int i=0; i < p.d_func()->elements.size(); ++i) {
2501 const QPainterPath::Element &e = p.d_func()->elements.at(i);
2503 s <<
double(e.x) <<
double(e.y);
2505 s << p.d_func()->cStart;
2506 s <<
int(p.fillRule());
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2528 bool errorDetected =
false;
2538 s.setStatus(QDataStream::ReadCorruptData);
2544 QList<QPainterPath::Element> &elements = p.d_func()->elements;
2546 bool folded =
false;
2547 int curveDataPending = 0;
2548 for (
int i=0; i<size; ++i) {
2554 if (type < QPainterPath::MoveToElement || type > QPainterPath::CurveToDataElement) {
2556 qWarning(
"QDataStream::operator>>: Invalid QPainterPath element type read, skipping it");
2558 errorDetected =
true;
2561 if (!isValidCoord(qreal(x)) || !isValidCoord(qreal(y))) {
2563 qWarning(
"QDataStream::operator>>: Invalid QPainterPath coordinates read, skipping it");
2565 errorDetected =
true;
2568 QPainterPath::Element elm = { qreal(x), qreal(y), QPainterPath::ElementType(type) };
2572 if (curveDataPending > 0) {
2573 if (elm.type != QPainterPath::CurveToDataElement) {
2574 errorDetected =
true;
2578 }
else if (elm.type == QPainterPath::CurveToDataElement
2579 || (elements.isEmpty() && elm.type != QPainterPath::MoveToElement)) {
2580 errorDetected =
true;
2582 }
else if (elm.type == QPainterPath::CurveToElement) {
2583 curveDataPending = 2;
2584 }
else if (elm.type == QPainterPath::MoveToElement && !elements.isEmpty()
2585 && elements.last().type == QPainterPath::MoveToElement) {
2586 elements.last() = elm;
2590 elements.append(elm);
2592 if (curveDataPending > 0)
2593 errorDetected =
true;
2598 if (fillRule != Qt::OddEvenFill && fillRule != Qt::WindingFill)
2599 errorDetected =
true;
2600 if (errorDetected || elements.isEmpty()) {
2602 qWarning(
"QDataStream::operator>>: Invalid QPainterPath read, resetting it");
2605 s.setStatus(QDataStream::ReadCorruptData);
2608 p.d_func()->hasWindingFill = (Qt::FillRule(fillRule) == Qt::WindingFill);
2612 if (folded || cStart < 0 || cStart >= elements.size()
2613 || elements.at(cStart).type != QPainterPath::MoveToElement) {
2614 cStart = elements.size() - 1;
2615 while (elements.at(cStart).type != QPainterPath::MoveToElement)
2618 p.d_func()->cStart = cStart;
2625
2626
2630 ((QPainterPath *) data)->moveTo(qt_fixed_to_real(x), qt_fixed_to_real(y));
2635 ((QPainterPath *) data)->lineTo(qt_fixed_to_real(x), qt_fixed_to_real(y));
2639 qfixed c2x, qfixed c2y,
2640 qfixed ex, qfixed ey,
2643 ((QPainterPath *) data)->cubicTo(qt_fixed_to_real(c1x), qt_fixed_to_real(c1y),
2644 qt_fixed_to_real(c2x), qt_fixed_to_real(c2y),
2645 qt_fixed_to_real(ex), qt_fixed_to_real(ey));
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2702 stroker.setMoveToHook(qt_path_stroke_move_to);
2703 stroker.setLineToHook(qt_path_stroke_line_to);
2704 stroker.setCubicToHook(qt_path_stroke_cubic_to);
2708
2709
2710QPainterPathStroker::QPainterPathStroker()
2711 : d_ptr(
new QPainterPathStrokerPrivate)
2716
2717
2718
2719
2720QPainterPathStroker::QPainterPathStroker(
const QPen &pen)
2721 : d_ptr(
new QPainterPathStrokerPrivate)
2723 setWidth(pen.widthF());
2724 setCapStyle(pen.capStyle());
2725 setJoinStyle(pen.joinStyle());
2726 setMiterLimit(pen.miterLimit());
2727 setDashOffset(pen.dashOffset());
2729 if (pen.style() == Qt::CustomDashLine)
2730 setDashPattern(pen.dashPattern());
2732 setDashPattern(pen.style());
2736
2737
2738QPainterPathStroker::~QPainterPathStroker()
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756QPainterPath QPainterPathStroker::createStroke(
const QPainterPath &path)
const
2758 QPainterPathStrokerPrivate *d =
const_cast<QPainterPathStrokerPrivate *>(d_func());
2759 QPainterPath stroke;
2762 if (d->dashPattern.isEmpty()) {
2763 d->stroker.strokePath(path, &stroke, QTransform());
2765 QDashStroker dashStroker(&d->stroker);
2766 dashStroker.setDashPattern(d->dashPattern);
2767 dashStroker.setDashOffset(d->dashOffset);
2768 dashStroker.setClipRect(d->stroker.clipRect());
2769 if (d->stroker.curveThresholdExplicitlySet())
2770 dashStroker.setCurveThreshold(d->stroker.curveThreshold());
2771 dashStroker.strokePath(path, &stroke, QTransform());
2773 stroke.setFillRule(Qt::WindingFill);
2778
2779
2780
2781
2782
2783void QPainterPathStroker::setWidth(qreal width)
2785 Q_D(QPainterPathStroker);
2788 d->stroker.setStrokeWidth(qt_real_to_fixed(width));
2792
2793
2794qreal QPainterPathStroker::width()
const
2796 return qt_fixed_to_real(d_func()->stroker.strokeWidth());
2801
2802
2803
2804
2805void QPainterPathStroker::setCapStyle(Qt::PenCapStyle style)
2807 d_func()->stroker.setCapStyle(style);
2812
2813
2814Qt::PenCapStyle QPainterPathStroker::capStyle()
const
2816 return d_func()->stroker.capStyle();
2820
2821
2822void QPainterPathStroker::setJoinStyle(Qt::PenJoinStyle style)
2824 d_func()->stroker.setJoinStyle(style);
2828
2829
2830Qt::PenJoinStyle QPainterPathStroker::joinStyle()
const
2832 return d_func()->stroker.joinStyle();
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845void QPainterPathStroker::setMiterLimit(qreal limit)
2847 d_func()->stroker.setMiterLimit(qt_real_to_fixed(limit));
2851
2852
2853qreal QPainterPathStroker::miterLimit()
const
2855 return qt_fixed_to_real(d_func()->stroker.miterLimit());
2860
2861
2862
2863
2864
2865
2866
2867void QPainterPathStroker::setCurveThreshold(qreal threshold)
2869 d_func()->stroker.setCurveThreshold(qt_real_to_fixed(threshold));
2873
2874
2875
2876qreal QPainterPathStroker::curveThreshold()
const
2878 return qt_fixed_to_real(d_func()->stroker.curveThreshold());
2882
2883
2884void QPainterPathStroker::setDashPattern(Qt::PenStyle style)
2886 d_func()->dashPattern = QDashStroker::patternForStyle(style);
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905void QPainterPathStroker::setDashPattern(
const QList<qreal> &dashPattern)
2907 d_func()->dashPattern.clear();
2908 for (
int i=0; i<dashPattern.size(); ++i)
2909 d_func()->dashPattern << qt_real_to_fixed(dashPattern.at(i));
2913
2914
2915QList<qreal> QPainterPathStroker::dashPattern()
const
2917 return d_func()->dashPattern;
2921
2922
2923qreal QPainterPathStroker::dashOffset()
const
2925 return d_func()->dashOffset;
2929
2930
2931
2932
2933
2934void QPainterPathStroker::setDashOffset(qreal offset)
2936 d_func()->dashOffset = offset;
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954QPolygonF QPainterPath::toFillPolygon(
const QTransform &matrix)
const
2956 const QList<QPolygonF> flats = toSubpathPolygons(matrix);
2958 if (flats.isEmpty())
2960 QPointF first = flats.first().first();
2961 for (
int i=0; i<flats.size(); ++i) {
2962 polygon += flats.at(i);
2963 if (!flats.at(i).isClosed())
2964 polygon += flats.at(i).first();
2972
2973
2974
2975
2976
2977bool QPainterPath::isCachingEnabled()
const
2980 return d && d->cacheEnabled;
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995void QPainterPath::setCachingEnabled(
bool enabled)
2998 if (d_func()->cacheEnabled == enabled)
3001 QPainterPathPrivate *d = d_func();
3002 d->cacheEnabled = enabled;
3004 d->m_runLengths.clear();
3005 d->m_runLengths.squeeze();
3012 return 3*t*t*(d - 3*c + 3*b - a) + 6*t*(c - 2*b + a) + 3*(b - a);
3016
3017
3018qreal QPainterPath::length()
const
3023 if (d->cacheEnabled) {
3024 if (d->dirtyRunLengths)
3025 d->computeRunLengths();
3026 return d->m_runLengths.last();
3030 for (
int i=1; i<d->elements.size(); ++i) {
3031 const Element &e = d->elements.at(i);
3038 len += QLineF(d->elements.at(i-1), e).length();
3041 case CurveToElement:
3043 QBezier b = QBezier::fromPoints(d->elements.at(i-1),
3045 d->elements.at(i+1),
3046 d->elements.at(i+2));
3059
3060
3061
3062
3063
3064
3065
3066qreal QPainterPath::percentAtLength(qreal len)
const
3069 if (isEmpty() || len <= 0)
3072 qreal totalLength = length();
3073 if (len > totalLength)
3076 Q_ASSERT(totalLength != 0);
3078 if (d->cacheEnabled) {
3079 const int ei = qMax(d->elementAtT(len / totalLength), 1);
3081 const QPainterPath::Element &e = d->elements[ei];
3083 case QPainterPath::LineToElement:
3084 res = len / totalLength;
3086 case CurveToElement:
3088 QBezier b = QBezier::fromPoints(d->elements.at(ei-1),
3090 d->elements.at(ei+1),
3091 d->elements.at(ei+2));
3092 qreal prevLen = d->m_runLengths[ei - 1];
3093 qreal blen = d->m_runLengths[ei] - prevLen;
3094 qreal elemRes = b.tAtLength(len - prevLen);
3095 res = (elemRes * blen + prevLen) / totalLength;
3105 for (
int i=1; i<d->elements.size(); ++i) {
3106 const Element &e = d->elements.at(i);
3113 QLineF line(d->elements.at(i-1), e);
3114 qreal llen = line.length();
3116 if (curLen >= len) {
3117 return len/totalLength ;
3122 case CurveToElement:
3124 QBezier b = QBezier::fromPoints(d->elements.at(i-1),
3126 d->elements.at(i+1),
3127 d->elements.at(i+2));
3128 qreal blen = b.length();
3129 qreal prevLen = curLen;
3132 if (curLen >= len) {
3133 qreal res = b.tAtLength(len - prevLen);
3134 return (res * blen + prevLen)/totalLength;
3149 qreal *bezierLength)
3151 *startingLength = 0;
3156 qreal totalLength = path.length();
3158 const int lastElement = path.elementCount() - 1;
3159 for (
int i=0; i <= lastElement; ++i) {
3160 const QPainterPath::Element &e = path.elementAt(i);
3163 case QPainterPath::MoveToElement:
3165 case QPainterPath::LineToElement:
3167 QLineF line(path.elementAt(i-1), e);
3168 qreal llen = line.length();
3170 if (i == lastElement || curLen/totalLength >= t) {
3171 *bezierLength = llen;
3172 QPointF a = path.elementAt(i-1);
3173 QPointF delta = e - a;
3174 return QBezier::fromPoints(a, a + delta / 3, a + 2 * delta / 3, e);
3178 case QPainterPath::CurveToElement:
3180 QBezier b = QBezier::fromPoints(path.elementAt(i-1),
3182 path.elementAt(i+1),
3183 path.elementAt(i+2));
3184 qreal blen = b.length();
3187 if (i + 2 == lastElement || curLen/totalLength >= t) {
3188 *bezierLength = blen;
3198 *startingLength = curLen;
3204 qreal *bezierLength)
const
3206 Q_ASSERT(t >= 0 && t <= 1);
3208 if (!path.isEmpty() && d->cacheEnabled) {
3209 const int ei = qMax(d->elementAtT(t), 1);
3210 const qreal prevRunLength = d->m_runLengths[ei - 1];
3211 *startingLength = prevRunLength;
3212 *bezierLength = d->m_runLengths[ei] - prevRunLength;
3213 const QPointF prev = d->elements[ei - 1];
3214 const QPainterPath::Element &e = d->elements[ei];
3216 case QPainterPath::LineToElement:
3218 QPointF delta = (e - prev) / 3;
3219 return QBezier::fromPoints(prev, prev + delta, prev + 2 * delta, e);
3221 case QPainterPath::CurveToElement:
3222 return QBezier::fromPoints(prev, e, elements[ei + 1], elements[ei + 2]);
3229 return uncached_bezierAtT(path, t, startingLength, bezierLength);
3233
3234
3235
3236
3237
3238
3239
3240
3241QPointF QPainterPath::pointAtPercent(qreal t)
const
3243 if (t < 0 || t > 1) {
3244 qWarning(
"QPainterPath::pointAtPercent accepts only values between 0 and 1");
3248 if (!d_ptr || d_ptr->elements.size() == 0)
3251 if (d_ptr->elements.size() == 1)
3252 return d_ptr->elements.at(0);
3254 qreal totalLength = length();
3256 qreal bezierLen = 0;
3257 QBezier b = d_ptr->bezierAtT(*
this, t, &curLen, &bezierLen);
3258 Q_ASSERT(bezierLen != 0);
3259 qreal realT = (totalLength * t - curLen) / bezierLen;
3261 return b.pointAt(qBound(qreal(0), realT, qreal(1)));
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276qreal QPainterPath::angleAtPercent(qreal t)
const
3278 if (t < 0 || t > 1) {
3279 qWarning(
"QPainterPath::angleAtPercent accepts only values between 0 and 1");
3286 qreal totalLength = length();
3288 qreal bezierLen = 0;
3289 QBezier bez = d_ptr->bezierAtT(*
this, t, &curLen, &bezierLen);
3290 Q_ASSERT(bezierLen != 0);
3291 qreal realT = (totalLength * t - curLen) / bezierLen;
3293 qreal m1 = slopeAt(realT, bez.x1, bez.x2, bez.x3, bez.x4);
3294 qreal m2 = slopeAt(realT, bez.y1, bez.y2, bez.y3, bez.y4);
3296 return QLineF(0, 0, m1, m2).angle();
3301
3302
3303
3304
3305
3306
3307
3308
3309qreal QPainterPath::slopeAtPercent(qreal t)
const
3311 if (t < 0 || t > 1) {
3312 qWarning(
"QPainterPath::slopeAtPercent accepts only values between 0 and 1");
3319 qreal totalLength = length();
3321 qreal bezierLen = 0;
3322 QBezier bez = d_ptr->bezierAtT(*
this, t, &curLen, &bezierLen);
3323 Q_ASSERT(bezierLen != 0);
3324 qreal realT = (totalLength * t - curLen) / bezierLen;
3326 qreal m1 = slopeAt(realT, bez.x1, bez.x2, bez.x3, bez.x4);
3327 qreal m2 = slopeAt(realT, bez.y1, bez.y2, bez.y3, bez.y4);
3334 if (std::numeric_limits<qreal>::has_infinity) {
3335 slope = (m2 < 0) ? -std::numeric_limits<qreal>::infinity()
3336 : std::numeric_limits<qreal>::infinity();
3338 if (
sizeof(qreal) ==
sizeof(
double)) {
3339 return 1.79769313486231570e+308;
3341 return ((qreal)3.40282346638528860e+38);
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3366QPainterPath QPainterPath::trimmed(qreal fromFraction, qreal toFraction, qreal offset)
const
3372 if (!isCachingEnabled()) {
3373 QPainterPath copy(*
this);
3374 copy.setCachingEnabled(
true);
3375 return copy.trimmed(fromFraction, toFraction, offset);
3378 qreal f1 = qBound(qreal(0), fromFraction, qreal(1));
3379 qreal f2 = qBound(qreal(0), toFraction, qreal(1));
3380 if (qFuzzyIsNull(f1 - f2))
3381 return QPainterPath();
3384 if (qFuzzyCompare(f2 - f1, qreal(1)))
3388 res.setFillRule(fillRule());
3392 offset = std::modf(offset, &dummy);
3394 qreal of1 = f1 + offset;
3395 qreal of2 = f2 + offset;
3397 f1 = of1 < 0 ? of1 + 1 : of1;
3398 f2 = of2 + 1 > 1 ? of2 : of2 + 1;
3399 }
else if (offset > 0) {
3400 f1 = of1 - 1 < 0 ? of1 : of1 - 1;
3401 f2 = of2 > 1 ? of2 - 1 : of2;
3404 const bool wrapping = (f1 > f2);
3407 QPainterPathPrivate *d = d_func();
3408 if (d->dirtyRunLengths)
3409 d->computeRunLengths();
3410 const qreal totalLength = d->m_runLengths.last();
3411 if (qFuzzyIsNull(totalLength))
3414 const qreal l1 = f1 * totalLength;
3415 const qreal l2 = f2 * totalLength;
3416 const int e1 = d->elementAtLength(l1);
3417 const bool mustTrimE1 = !QtPrivate::fuzzyCompare(d->m_runLengths.at(e1), l1);
3418 const int e2 = d->elementAtLength(l2);
3419 const bool mustTrimE2 = !QtPrivate::fuzzyCompare(d->m_runLengths.at(e2), l2);
3422 if (e1 == e2 && !wrapping && mustTrimE1 && mustTrimE2) {
3424 d->appendSliceOfElement(&res, e1, l1, l2);
3428 d->appendEndOfElement(&res, e1, l1);
3430 res.moveTo(d->endPointOfElement(e1));
3433 int firstWholeElement = e1 + 1;
3434 int lastWholeElement = (mustTrimE2 ? e2 - 1 : e2);
3436 d->appendElementRange(&res, firstWholeElement, lastWholeElement);
3438 int lastIndex = d->elements.size() - 1;
3439 d->appendElementRange(&res, firstWholeElement, lastIndex);
3440 bool isClosed = (QPointF(d->elements.at(0)) == QPointF(d->elements.at(lastIndex)));
3442 d->appendElementRange(&res, (isClosed ? 1 : 0), lastWholeElement);
3447 d->appendStartOfElement(&res, e2, l2);
3454 qreal startLen, qreal endLen)
3456 Q_ASSERT(cacheEnabled);
3457 Q_ASSERT(!dirtyRunLengths);
3459 if (elemIdx <= 0 || elemIdx >= elements.size())
3462 const qreal prevLen = m_runLengths.at(elemIdx - 1);
3463 const qreal elemLen = m_runLengths.at(elemIdx) - prevLen;
3464 const qreal len1 = startLen - prevLen;
3465 const qreal len2 = endLen - prevLen;
3466 if (qFuzzyIsNull(elemLen))
3469 const QPointF pp = elements.at(elemIdx - 1);
3470 const QPainterPath::Element e = elements.at(elemIdx);
3473 QPointF p1 = (trimFlags & TrimStart) ? l.pointAt(len1 / elemLen) : pp;
3474 QPointF p2 = (trimFlags & TrimEnd) ? l.pointAt(len2 / elemLen) : e;
3478 }
else if (e.isCurveTo()) {
3479 Q_ASSERT(elemIdx < elements.size() - 2);
3480 QBezier b = QBezier::fromPoints(pp, e, elements.at(elemIdx + 1), elements.at(elemIdx + 2));
3481 qreal t1 = (trimFlags & TrimStart) ? b.tAtLength(len1) : 0.0;
3482 qreal t2 = (trimFlags & TrimEnd) ? b.tAtLength(len2) : 1.0;
3483 QBezier c = b.getSubRange(t1, t2);
3485 to->moveTo(c.pt1());
3486 to->cubicTo(c.pt2(), c.pt3(), c.pt4());
3494 if (first < 0 || first >= elements.size() || last < 0 || last >= elements.size())
3498 for (
int i = first; i <= last; i++) {
3499 const QPainterPath::Element &e = elements.at(i);
3501 case QPainterPath::MoveToElement:
3504 case QPainterPath::LineToElement:
3507 case QPainterPath::CurveToElement:
3508 Q_ASSERT(i < elements.size() - 2);
3509 to->cubicTo(e, elements.at(i + 1), elements.at(i + 2));
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533void QPainterPath::addRoundedRect(
const QRectF &rect, qreal xRadius, qreal yRadius,
3536 QRectF r = rect.normalized();
3541 if (mode == Qt::AbsoluteSize) {
3542 qreal w = r.width() / 2;
3543 qreal h = r.height() / 2;
3548 xRadius = 100 * qMin(xRadius, w) / w;
3553 yRadius = 100 * qMin(yRadius, h) / h;
3563 if (xRadius <= 0 || yRadius <= 0) {
3570 qreal w = r.width();
3571 qreal h = r.height();
3572 qreal rxx2 = w*xRadius/100;
3573 qreal ryy2 = h*yRadius/100;
3578 bool first = d_func()->elements.size() < 2;
3580 arcMoveTo(x, y, rxx2, ryy2, 180);
3581 arcTo(x, y, rxx2, ryy2, 180, -90);
3582 arcTo(x+w-rxx2, y, rxx2, ryy2, 90, -90);
3583 arcTo(x+w-rxx2, y+h-ryy2, rxx2, ryy2, 0, -90);
3584 arcTo(x, y+h-ryy2, rxx2, ryy2, 270, -90);
3587 d_func()->require_moveTo =
true;
3588 d_func()->convex = first;
3592
3593
3594
3595
3596
3597
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611QPainterPath QPainterPath::united(
const QPainterPath &p)
const
3613 if (isEmpty() || p.isEmpty())
3614 return isEmpty() ? p : *
this;
3615 QPathClipper clipper(*
this, p);
3616 return clipper.clip(QPathClipper::BoolOr);
3620
3621
3622
3623
3624
3625
3626QPainterPath QPainterPath::intersected(
const QPainterPath &p)
const
3628 if (isEmpty() || p.isEmpty())
3629 return QPainterPath();
3630 QPathClipper clipper(*
this, p);
3631 return clipper.clip(QPathClipper::BoolAnd);
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644QPainterPath QPainterPath::subtracted(
const QPainterPath &p)
const
3646 if (isEmpty() || p.isEmpty())
3648 QPathClipper clipper(*
this, p);
3649 return clipper.clip(QPathClipper::BoolSub);
3653
3654
3655
3656
3657
3658
3659
3660
3661QPainterPath QPainterPath::simplified()
const
3665 QPathClipper clipper(*
this, QPainterPath());
3666 return clipper.clip(QPathClipper::Simplify);
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680bool QPainterPath::intersects(
const QPainterPath &p)
const
3682 if (p.elementCount() == 1)
3683 return contains(p.elementAt(0));
3684 if (isEmpty() || p.isEmpty())
3686 QPathClipper clipper(*
this, p);
3687 return clipper.intersect();
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702bool QPainterPath::contains(
const QPainterPath &p)
const
3704 if (p.elementCount() == 1)
3705 return contains(p.elementAt(0));
3706 if (isEmpty() || p.isEmpty())
3708 QPathClipper clipper(*
this, p);
3709 return clipper.contains();
3712void QPainterPath::setDirty(
bool dirty)
3714 d_func()->pathConverter.reset();
3715 d_func()->dirtyBounds = dirty;
3716 d_func()->dirtyControlBounds = dirty;
3717 d_func()->dirtyRunLengths = dirty;
3718 d_func()->convex =
false;
3721void QPainterPath::computeBoundingRect()
const
3723 QPainterPathPrivate *d = d_func();
3724 d->dirtyBounds =
false;
3726 d->bounds = QRect();
3730 qreal minx, maxx, miny, maxy;
3731 minx = maxx = d->elements.at(0).x;
3732 miny = maxy = d->elements.at(0).y;
3733 for (
int i=1; i<d->elements.size(); ++i) {
3734 const Element &e = d->elements.at(i);
3739 if (e.x > maxx) maxx = e.x;
3740 else if (e.x < minx) minx = e.x;
3741 if (e.y > maxy) maxy = e.y;
3742 else if (e.y < miny) miny = e.y;
3744 case CurveToElement:
3746 QBezier b = QBezier::fromPoints(d->elements.at(i-1),
3748 d->elements.at(i+1),
3749 d->elements.at(i+2));
3750 QRectF r = qt_painterpath_bezier_extrema(b);
3751 qreal right = r.right();
3752 qreal bottom = r.bottom();
3753 if (r.x() < minx) minx = r.x();
3754 if (right > maxx) maxx = right;
3755 if (r.y() < miny) miny = r.y();
3756 if (bottom > maxy) maxy = bottom;
3764 d->bounds = QRectF(minx, miny, maxx - minx, maxy - miny);
3768void QPainterPath::computeControlPointRect()
const
3770 QPainterPathPrivate *d = d_func();
3771 d->dirtyControlBounds =
false;
3773 d->controlBounds = QRect();
3777 qreal minx, maxx, miny, maxy;
3778 minx = maxx = d->elements.at(0).x;
3779 miny = maxy = d->elements.at(0).y;
3780 for (
int i=1; i<d->elements.size(); ++i) {
3781 const Element &e = d->elements.at(i);
3782 if (e.x > maxx) maxx = e.x;
3783 else if (e.x < minx) minx = e.x;
3784 if (e.y > maxy) maxy = e.y;
3785 else if (e.y < miny) miny = e.y;
3787 d->controlBounds = QRectF(minx, miny, maxx - minx, maxy - miny);
3792 Q_ASSERT(!elements.isEmpty());
3794 m_runLengths.clear();
3795 const int numElems = elements.size();
3796 m_runLengths.reserve(numElems);
3798 QPointF runPt = elements[0];
3800 for (
int i = 0; i < numElems; i++) {
3801 QPainterPath::Element e = elements[i];
3803 case QPainterPath::LineToElement:
3804 runLen += QLineF(runPt, e).length();
3807 case QPainterPath::CurveToElement: {
3808 Q_ASSERT(i < numElems - 2);
3809 QPainterPath::Element ee = elements[i + 2];
3810 runLen += QBezier::fromPoints(runPt, e, elements[i + 1], ee).length();
3814 case QPainterPath::MoveToElement:
3817 case QPainterPath::CurveToDataElement:
3820 m_runLengths.append(runLen);
3822 Q_ASSERT(m_runLengths.size() == elements.size());
3824 dirtyRunLengths =
false;
3827#ifndef QT_NO_DEBUG_STREAM
3830 QDebugStateSaver saver(s);
3831 s.nospace() <<
"QPainterPath: Element count=" << p.elementCount() << Qt::endl;
3832 const char *types[] = {
"MoveTo",
"LineTo",
"CurveTo",
"CurveToData"};
3833 for (
int i=0; i<p.elementCount(); ++i) {
3834 s.nospace() <<
" -> " << types[p.elementAt(i).type] <<
"(x=" << p.elementAt(i).x <<
", y=" << p.elementAt(i).y <<
')' << Qt::endl;
QBezier bezierAtT(const QPainterPath &path, qreal t, qreal *startingLength, qreal *bezierLength) const
QPainterPathStrokerPrivate()
Combined button and popup list for selecting options.
QDebug operator<<(QDebug dbg, const QFileInfo &fi)
static bool hasValidCoords(QRectF r)
static qreal slopeAt(qreal t, qreal a, qreal b, qreal c, qreal d)
void qt_path_stroke_move_to(qfixed x, qfixed y, void *data)
static bool qt_painterpath_check_crossing(const QPainterPath *path, const QRectF &rect)
static QBezier uncached_bezierAtT(const QPainterPath &path, qreal t, qreal *startingLength, qreal *bezierLength)
static bool qt_isect_curve_horizontal(const QBezier &bezier, qreal y, qreal x1, qreal x2, int depth=0)
static void qt_painterpath_isect_line(const QPointF &p1, const QPointF &p2, const QPointF &pos, int *winding)
static QRectF qt_painterpath_bezier_extrema(const QBezier &b)
#define QT_BEZIER_CHECK_T(bezier, t)
static bool qt_painterpath_isect_line_rect(qreal x1, qreal y1, qreal x2, qreal y2, const QRectF &rect)
#define QT_BEZIER_C(bezier, coord)
void qt_find_ellipse_coords(const QRectF &r, qreal angle, qreal length, QPointF *startPoint, QPointF *endPoint)
void qt_path_stroke_cubic_to(qfixed c1x, qfixed c1y, qfixed c2x, qfixed c2y, qfixed ex, qfixed ey, void *data)
#define QT_BEZIER_A(bezier, coord)
static bool epsilonCompare(const QPointF &a, const QPointF &b, const QSizeF &epsilon)
#define QT_BEZIER_B(bezier, coord)
static QT_BEGIN_NAMESPACE bool isValidCoord(qreal c)
static void qt_painterpath_isect_curve(const QBezier &bezier, const QPointF &pt, int *winding, int depth=0)
static bool qt_isect_curve_vertical(const QBezier &bezier, qreal x, qreal y1, qreal y2, int depth=0)
static bool pointOnEdge(const QRectF &rect, const QPointF &point)
QPainterPath qt_stroke_dash(const QPainterPath &path, qreal *dashes, int dashCount)
static bool hasValidCoords(QPointF p)
void qt_path_stroke_line_to(qfixed x, qfixed y, void *data)
QDataStream & operator<<(QDataStream &stream, const QImage &image)
[0]
QDataStream & operator>>(QDataStream &stream, QImage &image)