| 1 | /**************************************************************************** | 
|---|
| 2 | ** | 
|---|
| 3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). | 
|---|
| 4 | ** All rights reserved. | 
|---|
| 5 | ** Contact: Nokia Corporation (qt-info@nokia.com) | 
|---|
| 6 | ** | 
|---|
| 7 | ** This file is part of the QtOpenGL module of the Qt Toolkit. | 
|---|
| 8 | ** | 
|---|
| 9 | ** $QT_BEGIN_LICENSE:LGPL$ | 
|---|
| 10 | ** Commercial Usage | 
|---|
| 11 | ** Licensees holding valid Qt Commercial licenses may use this file in | 
|---|
| 12 | ** accordance with the Qt Commercial License Agreement provided with the | 
|---|
| 13 | ** Software or, alternatively, in accordance with the terms contained in | 
|---|
| 14 | ** a written agreement between you and Nokia. | 
|---|
| 15 | ** | 
|---|
| 16 | ** GNU Lesser General Public License Usage | 
|---|
| 17 | ** Alternatively, this file may be used under the terms of the GNU Lesser | 
|---|
| 18 | ** General Public License version 2.1 as published by the Free Software | 
|---|
| 19 | ** Foundation and appearing in the file LICENSE.LGPL included in the | 
|---|
| 20 | ** packaging of this file.  Please review the following information to | 
|---|
| 21 | ** ensure the GNU Lesser General Public License version 2.1 requirements | 
|---|
| 22 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | 
|---|
| 23 | ** | 
|---|
| 24 | ** In addition, as a special exception, Nokia gives you certain additional | 
|---|
| 25 | ** rights.  These rights are described in the Nokia Qt LGPL Exception | 
|---|
| 26 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. | 
|---|
| 27 | ** | 
|---|
| 28 | ** GNU General Public License Usage | 
|---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU | 
|---|
| 30 | ** General Public License version 3.0 as published by the Free Software | 
|---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the | 
|---|
| 32 | ** packaging of this file.  Please review the following information to | 
|---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be | 
|---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html. | 
|---|
| 35 | ** | 
|---|
| 36 | ** If you have questions regarding the use of this file, please contact | 
|---|
| 37 | ** Nokia at qt-info@nokia.com. | 
|---|
| 38 | ** $QT_END_LICENSE$ | 
|---|
| 39 | ** | 
|---|
| 40 | ****************************************************************************/ | 
|---|
| 41 |  | 
|---|
| 42 | #include "qgl2pexvertexarray_p.h" | 
|---|
| 43 |  | 
|---|
| 44 | #include <private/qbezier_p.h> | 
|---|
| 45 |  | 
|---|
| 46 | QT_BEGIN_NAMESPACE | 
|---|
| 47 |  | 
|---|
| 48 | void QGL2PEXVertexArray::clear() | 
|---|
| 49 | { | 
|---|
| 50 | vertexArray.reset(); | 
|---|
| 51 | vertexArrayStops.reset(); | 
|---|
| 52 | boundingRectDirty = true; | 
|---|
| 53 | } | 
|---|
| 54 |  | 
|---|
| 55 |  | 
|---|
| 56 | QGLRect QGL2PEXVertexArray::boundingRect() const | 
|---|
| 57 | { | 
|---|
| 58 | if (boundingRectDirty) | 
|---|
| 59 | return QGLRect(0.0, 0.0, 0.0, 0.0); | 
|---|
| 60 | else | 
|---|
| 61 | return QGLRect(minX, minY, maxX, maxY); | 
|---|
| 62 | } | 
|---|
| 63 |  | 
|---|
| 64 | void QGL2PEXVertexArray::addClosingLine(int index) | 
|---|
| 65 | { | 
|---|
| 66 | QPointF point(vertexArray.at(index)); | 
|---|
| 67 | if (point != QPointF(vertexArray.last())) | 
|---|
| 68 | vertexArray.add(point); | 
|---|
| 69 | } | 
|---|
| 70 |  | 
|---|
| 71 | void QGL2PEXVertexArray::addCentroid(const QVectorPath &path, int subPathIndex) | 
|---|
| 72 | { | 
|---|
| 73 | const QPointF *const points = reinterpret_cast<const QPointF *>(path.points()); | 
|---|
| 74 | const QPainterPath::ElementType *const elements = path.elements(); | 
|---|
| 75 |  | 
|---|
| 76 | QPointF sum = points[subPathIndex]; | 
|---|
| 77 | int count = 1; | 
|---|
| 78 |  | 
|---|
| 79 | for (int i = subPathIndex + 1; i < path.elementCount() && (!elements || elements[i] != QPainterPath::MoveToElement); ++i) { | 
|---|
| 80 | sum += points[i]; | 
|---|
| 81 | ++count; | 
|---|
| 82 | } | 
|---|
| 83 |  | 
|---|
| 84 | const QPointF centroid = sum / qreal(count); | 
|---|
| 85 | vertexArray.add(centroid); | 
|---|
| 86 | } | 
|---|
| 87 |  | 
|---|
| 88 | void QGL2PEXVertexArray::addPath(const QVectorPath &path, GLfloat curveInverseScale, bool outline) | 
|---|
| 89 | { | 
|---|
| 90 | const QPointF* const points = reinterpret_cast<const QPointF*>(path.points()); | 
|---|
| 91 | const QPainterPath::ElementType* const elements = path.elements(); | 
|---|
| 92 |  | 
|---|
| 93 | if (boundingRectDirty) { | 
|---|
| 94 | minX = maxX = points[0].x(); | 
|---|
| 95 | minY = maxY = points[0].y(); | 
|---|
| 96 | boundingRectDirty = false; | 
|---|
| 97 | } | 
|---|
| 98 |  | 
|---|
| 99 | if (!outline && !path.isConvex()) | 
|---|
| 100 | addCentroid(path, 0); | 
|---|
| 101 |  | 
|---|
| 102 | int lastMoveTo = vertexArray.size(); | 
|---|
| 103 | vertexArray.add(points[0]); // The first element is always a moveTo | 
|---|
| 104 |  | 
|---|
| 105 | do { | 
|---|
| 106 | if (!elements) { | 
|---|
| 107 | //             qDebug("QVectorPath has no elements"); | 
|---|
| 108 | // If the path has a null elements pointer, the elements implicitly | 
|---|
| 109 | // start with a moveTo (already added) and continue with lineTos: | 
|---|
| 110 | for (int i=1; i<path.elementCount(); ++i) | 
|---|
| 111 | lineToArray(points[i].x(), points[i].y()); | 
|---|
| 112 |  | 
|---|
| 113 | break; | 
|---|
| 114 | } | 
|---|
| 115 | //         qDebug("QVectorPath has element types"); | 
|---|
| 116 |  | 
|---|
| 117 | for (int i=1; i<path.elementCount(); ++i) { | 
|---|
| 118 | switch (elements[i]) { | 
|---|
| 119 | case QPainterPath::MoveToElement: | 
|---|
| 120 | if (!outline) | 
|---|
| 121 | addClosingLine(lastMoveTo); | 
|---|
| 122 | //                qDebug("element[%d] is a MoveToElement", i); | 
|---|
| 123 | vertexArrayStops.add(vertexArray.size()); | 
|---|
| 124 | if (!outline) { | 
|---|
| 125 | if (!path.isConvex()) addCentroid(path, i); | 
|---|
| 126 | lastMoveTo = vertexArray.size(); | 
|---|
| 127 | } | 
|---|
| 128 | lineToArray(points[i].x(), points[i].y()); // Add the moveTo as a new vertex | 
|---|
| 129 | break; | 
|---|
| 130 | case QPainterPath::LineToElement: | 
|---|
| 131 | //                qDebug("element[%d] is a LineToElement", i); | 
|---|
| 132 | lineToArray(points[i].x(), points[i].y()); | 
|---|
| 133 | break; | 
|---|
| 134 | case QPainterPath::CurveToElement: { | 
|---|
| 135 | QBezier b = QBezier::fromPoints(*(((const QPointF *) points) + i - 1), | 
|---|
| 136 | points[i], | 
|---|
| 137 | points[i+1], | 
|---|
| 138 | points[i+2]); | 
|---|
| 139 | QRectF bounds = b.bounds(); | 
|---|
| 140 | // threshold based on same algorithm as in qtriangulatingstroker.cpp | 
|---|
| 141 | int threshold = qMin<float>(64, qMax(bounds.width(), bounds.height()) * 3.14f / (curveInverseScale * 6)); | 
|---|
| 142 | if (threshold < 3) threshold = 3; | 
|---|
| 143 | qreal one_over_threshold_minus_1 = qreal(1) / (threshold - 1); | 
|---|
| 144 | for (int t=0; t<threshold; ++t) { | 
|---|
| 145 | QPointF pt = b.pointAt(t * one_over_threshold_minus_1); | 
|---|
| 146 | lineToArray(pt.x(), pt.y()); | 
|---|
| 147 | } | 
|---|
| 148 | i += 2; | 
|---|
| 149 | break; } | 
|---|
| 150 | default: | 
|---|
| 151 | break; | 
|---|
| 152 | } | 
|---|
| 153 | } | 
|---|
| 154 | } while (0); | 
|---|
| 155 |  | 
|---|
| 156 | if (!outline) | 
|---|
| 157 | addClosingLine(lastMoveTo); | 
|---|
| 158 | vertexArrayStops.add(vertexArray.size()); | 
|---|
| 159 | } | 
|---|
| 160 |  | 
|---|
| 161 | void QGL2PEXVertexArray::lineToArray(const GLfloat x, const GLfloat y) | 
|---|
| 162 | { | 
|---|
| 163 | vertexArray.add(QGLPoint(x, y)); | 
|---|
| 164 |  | 
|---|
| 165 | if (x > maxX) | 
|---|
| 166 | maxX = x; | 
|---|
| 167 | else if (x < minX) | 
|---|
| 168 | minX = x; | 
|---|
| 169 | if (y > maxY) | 
|---|
| 170 | maxY = y; | 
|---|
| 171 | else if (y < minY) | 
|---|
| 172 | minY = y; | 
|---|
| 173 | } | 
|---|
| 174 |  | 
|---|
| 175 | QT_END_NAMESPACE | 
|---|