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 QtOpenGL 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 "qopengl2pexvertexarray_p.h" |
41 | |
42 | #include <private/qbezier_p.h> |
43 | |
44 | QT_BEGIN_NAMESPACE |
45 | |
46 | void QOpenGL2PEXVertexArray::clear() |
47 | { |
48 | vertexArray.reset(); |
49 | vertexArrayStops.reset(); |
50 | boundingRectDirty = true; |
51 | } |
52 | |
53 | |
54 | QOpenGLRect QOpenGL2PEXVertexArray::boundingRect() const |
55 | { |
56 | if (boundingRectDirty) |
57 | return QOpenGLRect(0.0, 0.0, 0.0, 0.0); |
58 | else |
59 | return QOpenGLRect(minX, minY, maxX, maxY); |
60 | } |
61 | |
62 | void QOpenGL2PEXVertexArray::addClosingLine(int index) |
63 | { |
64 | QPointF point(vertexArray.at(index)); |
65 | if (point != QPointF(vertexArray.last())) |
66 | vertexArray.add(point); |
67 | } |
68 | |
69 | void QOpenGL2PEXVertexArray::addCentroid(const QVectorPath &path, int subPathIndex) |
70 | { |
71 | const QPointF *const points = reinterpret_cast<const QPointF *>(path.points()); |
72 | const QPainterPath::ElementType *const elements = path.elements(); |
73 | |
74 | QPointF sum = points[subPathIndex]; |
75 | int count = 1; |
76 | |
77 | for (int i = subPathIndex + 1; i < path.elementCount() && (!elements || elements[i] != QPainterPath::MoveToElement); ++i) { |
78 | sum += points[i]; |
79 | ++count; |
80 | } |
81 | |
82 | const QPointF centroid = sum / qreal(count); |
83 | vertexArray.add(centroid); |
84 | } |
85 | |
86 | void QOpenGL2PEXVertexArray::addPath(const QVectorPath &path, GLfloat curveInverseScale, bool outline) |
87 | { |
88 | const QPointF* const points = reinterpret_cast<const QPointF*>(path.points()); |
89 | const QPainterPath::ElementType* const elements = path.elements(); |
90 | |
91 | if (boundingRectDirty) { |
92 | minX = maxX = points[0].x(); |
93 | minY = maxY = points[0].y(); |
94 | boundingRectDirty = false; |
95 | } |
96 | |
97 | if (!outline && !path.isConvex()) |
98 | addCentroid(path, 0); |
99 | |
100 | int lastMoveTo = vertexArray.size(); |
101 | vertexArray.add(points[0]); // The first element is always a moveTo |
102 | |
103 | do { |
104 | if (!elements) { |
105 | // qDebug("QVectorPath has no elements"); |
106 | // If the path has a null elements pointer, the elements implicitly |
107 | // start with a moveTo (already added) and continue with lineTos: |
108 | for (int i=1; i<path.elementCount(); ++i) |
109 | lineToArray(points[i].x(), points[i].y()); |
110 | |
111 | break; |
112 | } |
113 | // qDebug("QVectorPath has element types"); |
114 | |
115 | for (int i=1; i<path.elementCount(); ++i) { |
116 | switch (elements[i]) { |
117 | case QPainterPath::MoveToElement: |
118 | if (!outline) |
119 | addClosingLine(lastMoveTo); |
120 | // qDebug("element[%d] is a MoveToElement", i); |
121 | vertexArrayStops.add(vertexArray.size()); |
122 | if (!outline) { |
123 | if (!path.isConvex()) addCentroid(path, i); |
124 | lastMoveTo = vertexArray.size(); |
125 | } |
126 | lineToArray(points[i].x(), points[i].y()); // Add the moveTo as a new vertex |
127 | break; |
128 | case QPainterPath::LineToElement: |
129 | // qDebug("element[%d] is a LineToElement", i); |
130 | lineToArray(points[i].x(), points[i].y()); |
131 | break; |
132 | case QPainterPath::CurveToElement: { |
133 | QBezier b = QBezier::fromPoints(*(((const QPointF *) points) + i - 1), |
134 | points[i], |
135 | points[i+1], |
136 | points[i+2]); |
137 | QRectF bounds = b.bounds(); |
138 | // threshold based on same algorithm as in qtriangulatingstroker.cpp |
139 | int threshold = qMin<float>(64, qMax(bounds.width(), bounds.height()) * 3.14f / (curveInverseScale * 6)); |
140 | if (threshold < 3) threshold = 3; |
141 | qreal one_over_threshold_minus_1 = qreal(1) / (threshold - 1); |
142 | for (int t=0; t<threshold; ++t) { |
143 | QPointF pt = b.pointAt(t * one_over_threshold_minus_1); |
144 | lineToArray(pt.x(), pt.y()); |
145 | } |
146 | i += 2; |
147 | break; } |
148 | default: |
149 | break; |
150 | } |
151 | } |
152 | } while (0); |
153 | |
154 | if (!outline) |
155 | addClosingLine(lastMoveTo); |
156 | vertexArrayStops.add(vertexArray.size()); |
157 | } |
158 | |
159 | void QOpenGL2PEXVertexArray::lineToArray(const GLfloat x, const GLfloat y) |
160 | { |
161 | vertexArray.add(QOpenGLPoint(x, y)); |
162 | |
163 | if (x > maxX) |
164 | maxX = x; |
165 | else if (x < minX) |
166 | minX = x; |
167 | if (y > maxY) |
168 | maxY = y; |
169 | else if (y < minY) |
170 | minY = y; |
171 | } |
172 | |
173 | QT_END_NAMESPACE |
174 | |