1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the QtGui module of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
49 // This file is not part of the Qt API. It exists purely as an
50 // implementation detail. This header file may change from version to
51 // version without notice, or even be removed.
56 #include "QtGui/qpainterpath.h"
57 #include "private/qdatabuffer_p.h"
58 #include "private/qnumeric_p.h"
62 // #define QFIXED_IS_26_6
64 #if defined QFIXED_IS_26_6
66 #define qt_real_to_fixed(real) qfixed(real * 64)
67 #define qt_int_to_fixed(real) qfixed(int(real) << 6)
68 #define qt_fixed_to_real(fixed) qreal(fixed / qreal(64))
69 #define qt_fixed_to_int(fixed) int(fixed >> 6)
75 bool operator==(const qfixed2d &other) const { return x == other.x && y == other.y; }
77 #elif defined QFIXED_IS_32_32
78 typedef qint64 qfixed;
79 #define qt_real_to_fixed(real) qfixed(real * double(qint64(1) << 32))
80 #define qt_fixed_to_real(fixed) qreal(fixed / double(qint64(1) << 32))
86 bool operator==(const qfixed2d &other) const { return x == other.x && y == other.y; }
88 #elif defined QFIXED_IS_16_16
90 #define qt_real_to_fixed(real) qfixed(real * qreal(1 << 16))
91 #define qt_fixed_to_real(fixed) qreal(fixed / qreal(1 << 16))
97 bool operator==(const qfixed2d &other) const { return x == other.x && y == other.y; }
100 typedef qreal qfixed;
101 #define qt_real_to_fixed(real) qfixed(real)
102 #define qt_fixed_to_real(fixed) fixed
108 bool operator==(const qfixed2d &other) const { return qFuzzyCompare(x, other.x)
109 && qFuzzyCompare(y, other.y); }
113 #define QT_PATH_KAPPA 0.5522847498
115 QPointF qt_curves_for_arc(const QRectF &rect, qreal startAngle, qreal sweepLength,
116 QPointF *controlPoints, int *point_count);
118 qreal qt_t_for_arc_angle(qreal angle);
120 typedef void (*qStrokerMoveToHook)(qfixed x, qfixed y, void *data);
121 typedef void (*qStrokerLineToHook)(qfixed x, qfixed y, void *data);
122 typedef void (*qStrokerCubicToHook)(qfixed c1x, qfixed c1y,
123 qfixed c2x, qfixed c2y,
124 qfixed ex, qfixed ey,
128 Q_GUI_EXPORT bool qt_scaleForTransform(const QTransform &transform, qreal *scale);
130 class Q_GUI_EXPORT QStrokerOps
134 QPainterPath::ElementType type;
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; }
142 operator qfixed2d () { qfixed2d pt = { x, y }; return pt; }
146 virtual ~QStrokerOps();
148 void setMoveToHook(qStrokerMoveToHook moveToHook) { m_moveTo = moveToHook; }
149 void setLineToHook(qStrokerLineToHook lineToHook) { m_lineTo = lineToHook; }
150 void setCubicToHook(qStrokerCubicToHook cubicToHook) { m_cubicTo = cubicToHook; }
152 virtual void begin(void *customData);
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);
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);
164 QRectF clipRect() const { return m_clip_rect; }
165 void setClipRect(const QRectF &clip) { m_clip_rect = clip; }
167 void setCurveThresholdFromTransform(const QTransform &transform)
170 qt_scaleForTransform(transform, &scale);
171 m_dashThreshold = scale == 0 ? qreal(0.5) : (qreal(0.5) / scale);
174 void setCurveThreshold(qfixed threshold) { m_curveThreshold = threshold; }
175 qfixed curveThreshold() const { return m_curveThreshold; }
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);
182 virtual void processCurrentSubpath() = 0;
183 QDataBuffer<Element> m_elements;
186 qfixed m_curveThreshold;
187 qfixed m_dashThreshold;
190 qStrokerMoveToHook m_moveTo;
191 qStrokerLineToHook m_lineTo;
192 qStrokerCubicToHook m_cubicTo;
196 class Q_GUI_EXPORT QStroker : public QStrokerOps
212 void setStrokeWidth(qfixed width) { m_strokeWidth = width; }
213 qfixed strokeWidth() const { return m_strokeWidth; }
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; }
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; }
223 void setMiterLimit(qfixed length) { m_miterLimit = length; }
224 qfixed miterLimit() const { return m_miterLimit; }
226 void joinPoints(qfixed x, qfixed y, const QLineF &nextLine, LineJoinMode join);
227 inline void emitMoveTo(qfixed x, qfixed y);
228 inline void emitLineTo(qfixed x, qfixed y);
229 inline void emitCubicTo(qfixed c1x, qfixed c1y, qfixed c2x, qfixed c2y, qfixed ex, qfixed ey);
232 static Qt::PenCapStyle capForJoinMode(LineJoinMode mode);
233 static LineJoinMode joinModeForCap(Qt::PenCapStyle);
235 static Qt::PenJoinStyle joinForJoinMode(LineJoinMode mode);
236 static LineJoinMode joinModeForJoin(Qt::PenJoinStyle joinStyle);
238 virtual void processCurrentSubpath();
240 qfixed m_strokeWidth;
243 LineJoinMode m_capStyle;
244 LineJoinMode m_joinStyle;
253 class Q_GUI_EXPORT QDashStroker : public QStrokerOps
256 QDashStroker(QStroker *stroker);
258 QStroker *stroker() const { return m_stroker; }
260 static QVector<qfixed> patternForStyle(Qt::PenStyle style);
262 void setDashPattern(const QVector<qfixed> &dashPattern) { m_dashPattern = dashPattern; }
263 QVector<qfixed> dashPattern() const { return m_dashPattern; }
265 void setDashOffset(qreal offset) { m_dashOffset = offset; }
266 qreal dashOffset() const { return m_dashOffset; }
268 virtual void begin(void *data);
271 inline void setStrokeWidth(qreal width) { m_stroke_width = width; }
272 inline void setMiterLimit(qreal limit) { m_miter_limit = limit; }
275 virtual void processCurrentSubpath();
278 QVector<qfixed> m_dashPattern;
281 qreal m_stroke_width;
286 /*******************************************************************************
287 * QStrokerOps inline membmers
290 inline void QStrokerOps::emitMoveTo(qfixed x, qfixed y)
293 m_moveTo(x, y, m_customData);
296 inline void QStrokerOps::emitLineTo(qfixed x, qfixed y)
299 m_lineTo(x, y, m_customData);
302 inline void QStrokerOps::emitCubicTo(qfixed c1x, qfixed c1y, qfixed c2x, qfixed c2y, qfixed ex, qfixed ey)
305 m_cubicTo(c1x, c1y, c2x, c2y, ex, ey, m_customData);
308 inline void QStrokerOps::moveTo(qfixed x, qfixed y)
310 if (m_elements.size()>1)
311 processCurrentSubpath();
313 Element e = { QPainterPath::MoveToElement, x, y };
317 inline void QStrokerOps::lineTo(qfixed x, qfixed y)
319 Element e = { QPainterPath::LineToElement, x, y };
323 inline void QStrokerOps::cubicTo(qfixed x1, qfixed y1, qfixed x2, qfixed y2, qfixed ex, qfixed ey)
325 Element c1 = { QPainterPath::CurveToElement, x1, y1 };
326 Element c2 = { QPainterPath::CurveToDataElement, x2, y2 };
327 Element e = { QPainterPath::CurveToDataElement, ex, ey };
333 /*******************************************************************************
334 * QStroker inline members
336 inline void QStroker::emitMoveTo(qfixed x, qfixed y)
342 QStrokerOps::emitMoveTo(x, y);
345 inline void QStroker::emitLineTo(qfixed x, qfixed y)
351 QStrokerOps::emitLineTo(x, y);
354 inline void QStroker::emitCubicTo(qfixed c1x, qfixed c1y,
355 qfixed c2x, qfixed c2y,
356 qfixed ex, qfixed ey)
358 if (c2x == ex && c2y == ey) {
359 if (c1x == ex && c1y == ey) {
372 QStrokerOps::emitCubicTo(c1x, c1y, c2x, c2y, ex, ey);
375 /*******************************************************************************
376 * QDashStroker inline members
378 inline void QDashStroker::begin(void *data)
381 m_stroker->begin(data);
382 QStrokerOps::begin(data);
385 inline void QDashStroker::end()
394 #endif // QSTROKER_P_H