source: trunk/src/gui/painting/qstroker_p.h

Last change on this file was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

File size: 11.8 KB
Line 
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 QtGui 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#ifndef QSTROKER_P_H
43#define QSTROKER_P_H
44
45//
46// W A R N I N G
47// -------------
48//
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.
52//
53// We mean it.
54//
55
56#include "QtGui/qpainterpath.h"
57#include "private/qdatabuffer_p.h"
58#include "private/qnumeric_p.h"
59
60QT_BEGIN_NAMESPACE
61
62// #define QFIXED_IS_26_6
63
64#if defined QFIXED_IS_26_6
65typedef int qfixed;
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)
70struct qfixed2d
71{
72 qfixed x;
73 qfixed y;
74
75 bool operator==(const qfixed2d &other) const { return x == other.x && y == other.y; }
76};
77#elif defined QFIXED_IS_32_32
78typedef 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))
81struct qfixed2d
82{
83 qfixed x;
84 qfixed y;
85
86 bool operator==(const qfixed2d &other) const { return x == other.x && y == other.y; }
87};
88#elif defined QFIXED_IS_16_16
89typedef int qfixed;
90#define qt_real_to_fixed(real) qfixed(real * qreal(1 << 16))
91#define qt_fixed_to_real(fixed) qreal(fixed / qreal(1 << 16))
92struct qfixed2d
93{
94 qfixed x;
95 qfixed y;
96
97 bool operator==(const qfixed2d &other) const { return x == other.x && y == other.y; }
98};
99#else
100typedef qreal qfixed;
101#define qt_real_to_fixed(real) qfixed(real)
102#define qt_fixed_to_real(fixed) fixed
103struct qfixed2d
104{
105 qfixed x;
106 qfixed y;
107
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; }
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 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);
230
231protected:
232 static Qt::PenCapStyle capForJoinMode(LineJoinMode mode);
233 static LineJoinMode joinModeForCap(Qt::PenCapStyle);
234
235 static Qt::PenJoinStyle joinForJoinMode(LineJoinMode mode);
236 static LineJoinMode joinModeForJoin(Qt::PenJoinStyle joinStyle);
237
238 virtual void processCurrentSubpath();
239
240 qfixed m_strokeWidth;
241 qfixed m_miterLimit;
242
243 LineJoinMode m_capStyle;
244 LineJoinMode m_joinStyle;
245
246 qfixed m_back1X;
247 qfixed m_back1Y;
248
249 qfixed m_back2X;
250 qfixed m_back2Y;
251};
252
253class Q_GUI_EXPORT QDashStroker : public QStrokerOps
254{
255public:
256 QDashStroker(QStroker *stroker);
257
258 QStroker *stroker() const { return m_stroker; }
259
260 static QVector<qfixed> patternForStyle(Qt::PenStyle style);
261
262 void setDashPattern(const QVector<qfixed> &dashPattern) { m_dashPattern = dashPattern; }
263 QVector<qfixed> dashPattern() const { return m_dashPattern; }
264
265 void setDashOffset(qreal offset) { m_dashOffset = offset; }
266 qreal dashOffset() const { return m_dashOffset; }
267
268 virtual void begin(void *data);
269 virtual void end();
270
271 inline void setStrokeWidth(qreal width) { m_stroke_width = width; }
272 inline void setMiterLimit(qreal limit) { m_miter_limit = limit; }
273
274protected:
275 virtual void processCurrentSubpath();
276
277 QStroker *m_stroker;
278 QVector<qfixed> m_dashPattern;
279 qreal m_dashOffset;
280
281 qreal m_stroke_width;
282 qreal m_miter_limit;
283};
284
285
286/*******************************************************************************
287 * QStrokerOps inline membmers
288 */
289
290inline void QStrokerOps::emitMoveTo(qfixed x, qfixed y)
291{
292 Q_ASSERT(m_moveTo);
293 m_moveTo(x, y, m_customData);
294}
295
296inline void QStrokerOps::emitLineTo(qfixed x, qfixed y)
297{
298 Q_ASSERT(m_lineTo);
299 m_lineTo(x, y, m_customData);
300}
301
302inline void QStrokerOps::emitCubicTo(qfixed c1x, qfixed c1y, qfixed c2x, qfixed c2y, qfixed ex, qfixed ey)
303{
304 Q_ASSERT(m_cubicTo);
305 m_cubicTo(c1x, c1y, c2x, c2y, ex, ey, m_customData);
306}
307
308inline void QStrokerOps::moveTo(qfixed x, qfixed y)
309{
310 if (m_elements.size()>1)
311 processCurrentSubpath();
312 m_elements.reset();
313 Element e = { QPainterPath::MoveToElement, x, y };
314 m_elements.add(e);
315}
316
317inline void QStrokerOps::lineTo(qfixed x, qfixed y)
318{
319 Element e = { QPainterPath::LineToElement, x, y };
320 m_elements.add(e);
321}
322
323inline void QStrokerOps::cubicTo(qfixed x1, qfixed y1, qfixed x2, qfixed y2, qfixed ex, qfixed ey)
324{
325 Element c1 = { QPainterPath::CurveToElement, x1, y1 };
326 Element c2 = { QPainterPath::CurveToDataElement, x2, y2 };
327 Element e = { QPainterPath::CurveToDataElement, ex, ey };
328 m_elements.add(c1);
329 m_elements.add(c2);
330 m_elements.add(e);
331}
332
333/*******************************************************************************
334 * QStroker inline members
335 */
336inline void QStroker::emitMoveTo(qfixed x, qfixed y)
337{
338 m_back2X = m_back1X;
339 m_back2Y = m_back1Y;
340 m_back1X = x;
341 m_back1Y = y;
342 QStrokerOps::emitMoveTo(x, y);
343}
344
345inline void QStroker::emitLineTo(qfixed x, qfixed y)
346{
347 m_back2X = m_back1X;
348 m_back2Y = m_back1Y;
349 m_back1X = x;
350 m_back1Y = y;
351 QStrokerOps::emitLineTo(x, y);
352}
353
354inline void QStroker::emitCubicTo(qfixed c1x, qfixed c1y,
355 qfixed c2x, qfixed c2y,
356 qfixed ex, qfixed ey)
357{
358 if (c2x == ex && c2y == ey) {
359 if (c1x == ex && c1y == ey) {
360 m_back2X = m_back1X;
361 m_back2Y = m_back1Y;
362 } else {
363 m_back2X = c1x;
364 m_back2Y = c1y;
365 }
366 } else {
367 m_back2X = c2x;
368 m_back2Y = c2y;
369 }
370 m_back1X = ex;
371 m_back1Y = ey;
372 QStrokerOps::emitCubicTo(c1x, c1y, c2x, c2y, ex, ey);
373}
374
375/*******************************************************************************
376 * QDashStroker inline members
377 */
378inline void QDashStroker::begin(void *data)
379{
380 if (m_stroker)
381 m_stroker->begin(data);
382 QStrokerOps::begin(data);
383}
384
385inline void QDashStroker::end()
386{
387 QStrokerOps::end();
388 if (m_stroker)
389 m_stroker->end();
390}
391
392QT_END_NAMESPACE
393
394#endif // QSTROKER_P_H
Note: See TracBrowser for help on using the repository browser.