Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qopengl2pexvertexarray.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
6
7#include <private/qbezier_p.h>
8
10
12{
13 vertexArray.reset();
14 vertexArrayStops.reset();
15 boundingRectDirty = true;
16}
17
18
20{
21 if (boundingRectDirty)
22 return QOpenGLRect(0.0, 0.0, 0.0, 0.0);
23 else
24 return QOpenGLRect(minX, minY, maxX, maxY);
25}
26
27void QOpenGL2PEXVertexArray::addClosingLine(int index)
28{
29 QPointF point(vertexArray.at(index));
30 if (point != QPointF(vertexArray.last()))
31 vertexArray.add(point);
32}
33
34void QOpenGL2PEXVertexArray::addCentroid(const QVectorPath &path, int subPathIndex)
35{
36 const QPointF *const points = reinterpret_cast<const QPointF *>(path.points());
37 const QPainterPath::ElementType *const elements = path.elements();
38
39 QPointF sum = points[subPathIndex];
40 int count = 1;
41
42 for (int i = subPathIndex + 1; i < path.elementCount() && (!elements || elements[i] != QPainterPath::MoveToElement); ++i) {
43 sum += points[i];
44 ++count;
45 }
46
47 const QPointF centroid = sum / qreal(count);
48 vertexArray.add(centroid);
49}
50
51void QOpenGL2PEXVertexArray::addPath(const QVectorPath &path, GLfloat curveInverseScale, bool outline)
52{
53 const QPointF* const points = reinterpret_cast<const QPointF*>(path.points());
54 const QPainterPath::ElementType* const elements = path.elements();
55
56 if (boundingRectDirty) {
57 minX = maxX = points[0].x();
58 minY = maxY = points[0].y();
59 boundingRectDirty = false;
60 }
61
62 if (!outline && !path.isConvex())
63 addCentroid(path, 0);
64
65 int lastMoveTo = vertexArray.size();
66 vertexArray.add(points[0]); // The first element is always a moveTo
67
68 do {
69 if (!elements) {
70// qDebug("QVectorPath has no elements");
71 // If the path has a null elements pointer, the elements implicitly
72 // start with a moveTo (already added) and continue with lineTos:
73 for (int i=1; i<path.elementCount(); ++i)
74 lineToArray(points[i].x(), points[i].y());
75
76 break;
77 }
78// qDebug("QVectorPath has element types");
79
80 for (int i=1; i<path.elementCount(); ++i) {
81 switch (elements[i]) {
82 case QPainterPath::MoveToElement:
83 if (!outline)
84 addClosingLine(lastMoveTo);
85// qDebug("element[%d] is a MoveToElement", i);
86 vertexArrayStops.add(vertexArray.size());
87 if (!outline) {
88 if (!path.isConvex()) addCentroid(path, i);
89 lastMoveTo = vertexArray.size();
90 }
91 lineToArray(points[i].x(), points[i].y()); // Add the moveTo as a new vertex
92 break;
93 case QPainterPath::LineToElement:
94// qDebug("element[%d] is a LineToElement", i);
95 lineToArray(points[i].x(), points[i].y());
96 break;
97 case QPainterPath::CurveToElement: {
98 QBezier b = QBezier::fromPoints(*(((const QPointF *) points) + i - 1),
99 points[i],
100 points[i+1],
101 points[i+2]);
102 QRectF bounds = b.bounds();
103 // threshold based on same algorithm as in qtriangulatingstroker.cpp
104 int threshold = qMin<float>(64, qMax(bounds.width(), bounds.height()) * 3.14f / (curveInverseScale * 6));
105 if (threshold < 3) threshold = 3;
106 qreal one_over_threshold_minus_1 = qreal(1) / (threshold - 1);
107 for (int t=0; t<threshold; ++t) {
108 QPointF pt = b.pointAt(t * one_over_threshold_minus_1);
109 lineToArray(pt.x(), pt.y());
110 }
111 i += 2;
112 break; }
113 default:
114 break;
115 }
116 }
117 } while (0);
118
119 if (!outline)
120 addClosingLine(lastMoveTo);
121 vertexArrayStops.add(vertexArray.size());
122}
123
124void QOpenGL2PEXVertexArray::lineToArray(const GLfloat x, const GLfloat y)
125{
126 vertexArray.add(QOpenGLPoint(x, y));
127
128 if (x > maxX)
129 maxX = x;
130 else if (x < minX)
131 minX = x;
132 if (y > maxY)
133 maxY = y;
134 else if (y < minY)
135 minY = y;
136}
137
138QT_END_NAMESPACE
void addPath(const QVectorPath &path, GLfloat curveInverseScale, bool outline=true)
void lineToArray(const GLfloat x, const GLfloat y)
Combined button and popup list for selecting options.