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

source code of qtbase/src/opengl/qopengl2pexvertexarray.cpp