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 QSTROKER_P_H
41#define QSTROKER_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 "QtGui/qpainterpath.h"
56#include "private/qdatabuffer_p.h"
57#include "private/qnumeric_p.h"
58
59QT_BEGIN_NAMESPACE
60
61// #define QFIXED_IS_26_6
62
63#if defined QFIXED_IS_26_6
64typedef int qfixed;
65#define qt_real_to_fixed(real) qfixed(real * 64)
66#define qt_int_to_fixed(real) qfixed(int(real) << 6)
67#define qt_fixed_to_real(fixed) qreal(fixed / qreal(64))
68#define qt_fixed_to_int(fixed) int(fixed >> 6)
69struct qfixed2d
70{
71 qfixed x;
72 qfixed y;
73
74 bool operator==(const qfixed2d &other) const { return x == other.x && y == other.y; }
75};
76#elif defined QFIXED_IS_32_32
77typedef qint64 qfixed;
78#define qt_real_to_fixed(real) qfixed(real * double(qint64(1) << 32))
79#define qt_fixed_to_real(fixed) qreal(fixed / double(qint64(1) << 32))
80struct qfixed2d
81{
82 qfixed x;
83 qfixed y;
84
85 bool operator==(const qfixed2d &other) const { return x == other.x && y == other.y; }
86};
87#elif defined QFIXED_IS_16_16
88typedef int qfixed;
89#define qt_real_to_fixed(real) qfixed(real * qreal(1 << 16))
90#define qt_fixed_to_real(fixed) qreal(fixed / qreal(1 << 16))
91struct qfixed2d
92{
93 qfixed x;
94 qfixed y;
95
96 bool operator==(const qfixed2d &other) const { return x == other.x && y == other.y; }
97};
98#else
99typedef qreal qfixed;
100#define qt_real_to_fixed(real) qfixed(real)
101#define qt_fixed_to_real(fixed) fixed
102struct qfixed2d
103{
104 qfixed x;
105 qfixed y;
106
107 bool isFinite() { return qIsFinite(x) && qIsFinite(y); }
108 bool operator==(const qfixed2d &other) const { return qFuzzyCompare(x, other.x)
109 && qFuzzyCompare(y, other.y); }
110};
111#endif
112
113#define QT_PATH_KAPPA 0.5522847498
114
115QPointF qt_curves_for_arc(const QRectF &rect, qreal startAngle, qreal sweepLength,
116 QPointF *controlPoints, int *point_count);
117
118qreal qt_t_for_arc_angle(qreal angle);
119
120typedef void (*qStrokerMoveToHook)(qfixed x, qfixed y, void *data);
121typedef void (*qStrokerLineToHook)(qfixed x, qfixed y, void *data);
122typedef void (*qStrokerCubicToHook)(qfixed c1x, qfixed c1y,
123 qfixed c2x, qfixed c2y,
124 qfixed ex, qfixed ey,
125 void *data);
126
127// qtransform.cpp
128Q_GUI_EXPORT bool qt_scaleForTransform(const QTransform &transform, qreal *scale);
129
130class Q_GUI_EXPORT QStrokerOps
131{
132public:
133 struct Element {
134 QPainterPath::ElementType type;
135 qfixed x;
136 qfixed y;
137
138 inline bool isMoveTo() const { return type == QPainterPath::MoveToElement; }
139 inline bool isLineTo() const { return type == QPainterPath::LineToElement; }
140 inline bool isCurveTo() const { return type == QPainterPath::CurveToElement; }
141
142 operator qfixed2d () { qfixed2d pt = { x, y }; return pt; }
143 };
144
145 QStrokerOps();
146 virtual ~QStrokerOps();
147
148 void setMoveToHook(qStrokerMoveToHook moveToHook) { m_moveTo = moveToHook; }
149 void setLineToHook(qStrokerLineToHook lineToHook) { m_lineTo = lineToHook; }
150 void setCubicToHook(qStrokerCubicToHook cubicToHook) { m_cubicTo = cubicToHook; }
151
152 virtual void begin(void *customData);
153 virtual void end();
154
155 inline void moveTo(qfixed x, qfixed y);
156 inline void lineTo(qfixed x, qfixed y);
157 inline void cubicTo(qfixed x1, qfixed y1, qfixed x2, qfixed y2, qfixed ex, qfixed ey);
158
159 void strokePath(const QPainterPath &path, void *data, const QTransform &matrix);
160 void strokePolygon(const QPointF *points, int pointCount, bool implicit_close,
161 void *data, const QTransform &matrix);
162 void strokeEllipse(const QRectF &ellipse, void *data, const QTransform &matrix);
163
164 QRectF clipRect() const { return m_clip_rect; }
165 void setClipRect(const QRectF &clip) { m_clip_rect = clip; }
166
167 void setCurveThresholdFromTransform(const QTransform &transform)
168 {
169 qreal scale;
170 qt_scaleForTransform(transform, &scale);
171 m_dashThreshold = scale == 0 ? qreal(0.5) : (qreal(0.5) / scale);
172 }
173
174 void setCurveThreshold(qfixed threshold) { m_curveThreshold = threshold; }
175 qfixed curveThreshold() const { return m_curveThreshold; }
176
177protected:
178 inline void emitMoveTo(qfixed x, qfixed y);
179 inline void emitLineTo(qfixed x, qfixed y);
180 inline void emitCubicTo(qfixed c1x, qfixed c1y, qfixed c2x, qfixed c2y, qfixed ex, qfixed ey);
181
182 virtual void processCurrentSubpath() = 0;
183 QDataBuffer<Element> m_elements;
184
185 QRectF m_clip_rect;
186 qfixed m_curveThreshold;
187 qfixed m_dashThreshold;
188
189 void *m_customData;
190 qStrokerMoveToHook m_moveTo;
191 qStrokerLineToHook m_lineTo;
192 qStrokerCubicToHook m_cubicTo;
193
194};
195
196class Q_GUI_EXPORT QStroker : public QStrokerOps
197{
198public:
199
200 enum LineJoinMode {
201 FlatJoin,
202 SquareJoin,
203 MiterJoin,
204 RoundJoin,
205 RoundCap,
206 SvgMiterJoin
207 };
208
209 QStroker();
210 ~QStroker();
211
212 void setStrokeWidth(qfixed width) { m_strokeWidth = width; m_curveThreshold = qt_real_to_fixed(width > 4 ? 1.0/width : 0.25); }
213 qfixed strokeWidth() const { return m_strokeWidth; }
214
215 void setCapStyle(Qt::PenCapStyle capStyle) { m_capStyle = joinModeForCap(capStyle); }
216 Qt::PenCapStyle capStyle() const { return capForJoinMode(m_capStyle); }
217 LineJoinMode capStyleMode() const { return m_capStyle; }
218
219 void setJoinStyle(Qt::PenJoinStyle style) { m_joinStyle = joinModeForJoin(style); }
220 Qt::PenJoinStyle joinStyle() const { return joinForJoinMode(m_joinStyle); }
221 LineJoinMode joinStyleMode() const { return m_joinStyle; }
222
223 void setMiterLimit(qfixed length) { m_miterLimit = length; }
224 qfixed miterLimit() const { return m_miterLimit; }
225
226 void setForceOpen(bool state) { m_forceOpen = state; }
227 bool forceOpen() { return m_forceOpen; }
228
229 void joinPoints(qfixed x, qfixed y, const QLineF &nextLine, LineJoinMode join);
230 inline void emitMoveTo(qfixed x, qfixed y);
231 inline void emitLineTo(qfixed x, qfixed y);
232 inline void emitCubicTo(qfixed c1x, qfixed c1y, qfixed c2x, qfixed c2y, qfixed ex, qfixed ey);
233
234protected:
235 static Qt::PenCapStyle capForJoinMode(LineJoinMode mode);
236 static LineJoinMode joinModeForCap(Qt::PenCapStyle);
237
238 static Qt::PenJoinStyle joinForJoinMode(LineJoinMode mode);
239 static LineJoinMode joinModeForJoin(Qt::PenJoinStyle joinStyle);
240
241 void processCurrentSubpath() override;
242
243 qfixed m_strokeWidth;
244 qfixed m_miterLimit;
245
246 LineJoinMode m_capStyle;
247 LineJoinMode m_joinStyle;
248
249 qfixed m_back1X;
250 qfixed m_back1Y;
251
252 qfixed m_back2X;
253 qfixed m_back2Y;
254
255 bool m_forceOpen;
256};
257
258class Q_GUI_EXPORT QDashStroker : public QStrokerOps
259{
260public:
261 QDashStroker(QStroker *stroker);
262 ~QDashStroker();
263
264 QStroker *stroker() const { return m_stroker; }
265
266 static QVector<qfixed> patternForStyle(Qt::PenStyle style);
267
268 void setDashPattern(const QVector<qfixed> &dashPattern) { m_dashPattern = dashPattern; }
269 QVector<qfixed> dashPattern() const { return m_dashPattern; }
270
271 void setDashOffset(qreal offset) { m_dashOffset = offset; }
272 qreal dashOffset() const { return m_dashOffset; }
273
274 void begin(void *data) override;
275 void end() override;
276
277 inline void setStrokeWidth(qreal width) { m_stroke_width = width; }
278 inline void setMiterLimit(qreal limit) { m_miter_limit = limit; }
279
280protected:
281 void processCurrentSubpath() override;
282
283 QStroker *m_stroker;
284 QVector<qfixed> m_dashPattern;
285 qreal m_dashOffset;
286
287 qreal m_stroke_width;
288 qreal m_miter_limit;
289};
290
291
292/*******************************************************************************
293 * QStrokerOps inline membmers
294 */
295
296inline void QStrokerOps::emitMoveTo(qfixed x, qfixed y)
297{
298 Q_ASSERT(m_moveTo);
299 m_moveTo(x, y, m_customData);
300}
301
302inline void QStrokerOps::emitLineTo(qfixed x, qfixed y)
303{
304 Q_ASSERT(m_lineTo);
305 m_lineTo(x, y, m_customData);
306}
307
308inline void QStrokerOps::emitCubicTo(qfixed c1x, qfixed c1y, qfixed c2x, qfixed c2y, qfixed ex, qfixed ey)
309{
310 Q_ASSERT(m_cubicTo);
311 m_cubicTo(c1x, c1y, c2x, c2y, ex, ey, m_customData);
312}
313
314inline void QStrokerOps::moveTo(qfixed x, qfixed y)
315{
316 if (m_elements.size()>1)
317 processCurrentSubpath();
318 m_elements.reset();
319 Element e = { QPainterPath::MoveToElement, x, y };
320 m_elements.add(e);
321}
322
323inline void QStrokerOps::lineTo(qfixed x, qfixed y)
324{
325 Element e = { QPainterPath::LineToElement, x, y };
326 m_elements.add(e);
327}
328
329inline void QStrokerOps::cubicTo(qfixed x1, qfixed y1, qfixed x2, qfixed y2, qfixed ex, qfixed ey)
330{
331 Element c1 = { QPainterPath::CurveToElement, x1, y1 };
332 Element c2 = { QPainterPath::CurveToDataElement, x2, y2 };
333 Element e = { QPainterPath::CurveToDataElement, ex, ey };
334 m_elements.add(c1);
335 m_elements.add(c2);
336 m_elements.add(e);
337}
338
339/*******************************************************************************
340 * QStroker inline members
341 */
342inline void QStroker::emitMoveTo(qfixed x, qfixed y)
343{
344 m_back2X = m_back1X;
345 m_back2Y = m_back1Y;
346 m_back1X = x;
347 m_back1Y = y;
348 QStrokerOps::emitMoveTo(x, y);
349}
350
351inline void QStroker::emitLineTo(qfixed x, qfixed y)
352{
353 m_back2X = m_back1X;
354 m_back2Y = m_back1Y;
355 m_back1X = x;
356 m_back1Y = y;
357 QStrokerOps::emitLineTo(x, y);
358}
359
360inline void QStroker::emitCubicTo(qfixed c1x, qfixed c1y,
361 qfixed c2x, qfixed c2y,
362 qfixed ex, qfixed ey)
363{
364 if (c2x == ex && c2y == ey) {
365 if (c1x == ex && c1y == ey) {
366 m_back2X = m_back1X;
367 m_back2Y = m_back1Y;
368 } else {
369 m_back2X = c1x;
370 m_back2Y = c1y;
371 }
372 } else {
373 m_back2X = c2x;
374 m_back2Y = c2y;
375 }
376 m_back1X = ex;
377 m_back1Y = ey;
378 QStrokerOps::emitCubicTo(c1x, c1y, c2x, c2y, ex, ey);
379}
380
381/*******************************************************************************
382 * QDashStroker inline members
383 */
384inline void QDashStroker::begin(void *data)
385{
386 if (m_stroker)
387 m_stroker->begin(data);
388 QStrokerOps::begin(data);
389}
390
391inline void QDashStroker::end()
392{
393 QStrokerOps::end();
394 if (m_stroker)
395 m_stroker->end();
396}
397
398QT_END_NAMESPACE
399
400#endif // QSTROKER_P_H
401