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 QtGui 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 #ifndef QTRIANGULATINGSTROKER_P_H
41 #define QTRIANGULATINGSTROKER_P_H
42 
43 //
44 //  W A R N I N G
45 //  -------------
46 //
47 // This file is not part of the Qt API.  It exists purely as an
48 // implementation detail.  This header file may change from version to
49 // version without notice, or even be removed.
50 //
51 // We mean it.
52 //
53 
54 #include <QtGui/private/qtguiglobal_p.h>
55 #include <private/qdatabuffer_p.h>
56 #include <qvarlengtharray.h>
57 #include <private/qvectorpath_p.h>
58 #include <private/qbezier_p.h>
59 #include <private/qnumeric_p.h>
60 #include <private/qmath_p.h>
61 
62 QT_BEGIN_NAMESPACE
63 
64 class Q_GUI_EXPORT QTriangulatingStroker
65 {
66 public:
QTriangulatingStroker()67     QTriangulatingStroker() : m_vertices(0), m_cx(0), m_cy(0), m_nvx(0), m_nvy(0), m_width(1), m_miter_limit(2),
68         m_roundness(0), m_sin_theta(0), m_cos_theta(0), m_inv_scale(1), m_curvyness_mul(1), m_curvyness_add(0),
69         m_join_style(Qt::BevelJoin), m_cap_style(Qt::SquareCap) {}
70 
71     void process(const QVectorPath &path, const QPen &pen, const QRectF &clip, QPainter::RenderHints hints);
72 
vertexCount()73     inline int vertexCount() const { return m_vertices.size(); }
vertices()74     inline const float *vertices() const { return m_vertices.data(); }
75 
setInvScale(qreal invScale)76     inline void setInvScale(qreal invScale) { m_inv_scale = invScale; }
77 
78 private:
79     inline void emitLineSegment(float x, float y, float nx, float ny);
80     void moveTo(const qreal *pts);
81     inline void lineTo(const qreal *pts);
82     void cubicTo(const qreal *pts);
83     void join(const qreal *pts);
84     inline void normalVector(float x1, float y1, float x2, float y2, float *nx, float *ny);
85     void endCap(const qreal *pts);
86     void arcPoints(float cx, float cy, float fromX, float fromY, float toX, float toY, QVarLengthArray<float> &points);
87     void endCapOrJoinClosed(const qreal *start, const qreal *cur, bool implicitClose, bool endsAtStart);
88 
89 
90     QDataBuffer<float> m_vertices;
91 
92     float m_cx, m_cy;           // current points
93     float m_nvx, m_nvy;         // normal vector...
94     float m_width;
95     qreal m_miter_limit;
96 
97     int m_roundness;            // Number of line segments in a round join
98     qreal m_sin_theta;          // sin(m_roundness / 360);
99     qreal m_cos_theta;          // cos(m_roundness / 360);
100     qreal m_inv_scale;
101     float m_curvyness_mul;
102     float m_curvyness_add;
103 
104     Qt::PenJoinStyle m_join_style;
105     Qt::PenCapStyle m_cap_style;
106 };
107 
108 class Q_GUI_EXPORT QDashedStrokeProcessor
109 {
110 public:
111     QDashedStrokeProcessor();
112 
113     void process(const QVectorPath &path, const QPen &pen, const QRectF &clip, QPainter::RenderHints hints);
114 
addElement(QPainterPath::ElementType type,qreal x,qreal y)115     inline void addElement(QPainterPath::ElementType type, qreal x, qreal y) {
116         m_points.add(x);
117         m_points.add(y);
118         m_types.add(type);
119     }
120 
elementCount()121     inline int elementCount() const { return m_types.size(); }
points()122     inline qreal *points() const { return m_points.data(); }
elementTypes()123     inline QPainterPath::ElementType *elementTypes() const { return m_types.data(); }
124 
setInvScale(qreal invScale)125     inline void setInvScale(qreal invScale) { m_inv_scale = invScale; }
126 
127 private:
128     QDataBuffer<qreal> m_points;
129     QDataBuffer<QPainterPath::ElementType> m_types;
130     QDashStroker m_dash_stroker;
131     qreal m_inv_scale;
132 };
133 
normalVector(float x1,float y1,float x2,float y2,float * nx,float * ny)134 inline void QTriangulatingStroker::normalVector(float x1, float y1, float x2, float y2,
135                                                 float *nx, float *ny)
136 {
137     float dx = x2 - x1;
138     float dy = y2 - y1;
139     Q_ASSERT(dx != 0 || dy != 0);
140 
141     float pw;
142 
143     if (dx == 0)
144         pw = m_width / std::abs(dy);
145     else if (dy == 0)
146         pw = m_width / std::abs(dx);
147     else
148         pw = m_width / std::sqrt(dx*dx + dy*dy);
149 
150     *nx = -dy * pw;
151     *ny = dx * pw;
152 }
153 
emitLineSegment(float x,float y,float vx,float vy)154 inline void QTriangulatingStroker::emitLineSegment(float x, float y, float vx, float vy)
155 {
156     m_vertices.add(x + vx);
157     m_vertices.add(y + vy);
158     m_vertices.add(x - vx);
159     m_vertices.add(y - vy);
160 }
161 
lineTo(const qreal * pts)162 void QTriangulatingStroker::lineTo(const qreal *pts)
163 {
164     emitLineSegment(pts[0], pts[1], m_nvx, m_nvy);
165     m_cx = pts[0];
166     m_cy = pts[1];
167 }
168 
169 QT_END_NAMESPACE
170 
171 #endif
172