1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** Contact: Qt Software Information ([email protected])
|
---|
5 | **
|
---|
6 | ** This file is part of the QtGui module of the Qt Toolkit.
|
---|
7 | **
|
---|
8 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
9 | ** Commercial Usage
|
---|
10 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
11 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
12 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
13 | ** a written agreement between you and Nokia.
|
---|
14 | **
|
---|
15 | ** GNU Lesser General Public License Usage
|
---|
16 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
17 | ** General Public License version 2.1 as published by the Free Software
|
---|
18 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
19 | ** packaging of this file. Please review the following information to
|
---|
20 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
22 | **
|
---|
23 | ** In addition, as a special exception, Nokia gives you certain
|
---|
24 | ** additional rights. These rights are described in the Nokia Qt LGPL
|
---|
25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
---|
26 | ** 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 are unsure which license is appropriate for your use, please
|
---|
37 | ** contact the sales department at [email protected].
|
---|
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 |
|
---|
60 | QT_BEGIN_NAMESPACE
|
---|
61 |
|
---|
62 | // #define QFIXED_IS_26_6
|
---|
63 |
|
---|
64 | #if defined QFIXED_IS_26_6
|
---|
65 | typedef 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)
|
---|
70 | struct 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
|
---|
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))
|
---|
81 | struct 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
|
---|
89 | typedef 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))
|
---|
92 | struct 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
|
---|
100 | typedef qreal qfixed;
|
---|
101 | #define qt_real_to_fixed(real) qfixed(real)
|
---|
102 | #define qt_fixed_to_real(fixed) fixed
|
---|
103 | struct 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 |
|
---|
115 | QPointF qt_curves_for_arc(const QRectF &rect, qreal startAngle, qreal sweepLength,
|
---|
116 | QPointF *controlPoints, int *point_count);
|
---|
117 |
|
---|
118 | qreal qt_t_for_arc_angle(qreal angle);
|
---|
119 |
|
---|
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,
|
---|
125 | void *data);
|
---|
126 |
|
---|
127 | class Q_GUI_EXPORT QStrokerOps
|
---|
128 | {
|
---|
129 | public:
|
---|
130 | struct Element {
|
---|
131 | QPainterPath::ElementType type;
|
---|
132 | qfixed x;
|
---|
133 | qfixed y;
|
---|
134 |
|
---|
135 | inline bool isMoveTo() const { return type == QPainterPath::MoveToElement; }
|
---|
136 | inline bool isLineTo() const { return type == QPainterPath::LineToElement; }
|
---|
137 | inline bool isCurveTo() const { return type == QPainterPath::CurveToElement; }
|
---|
138 |
|
---|
139 | operator qfixed2d () { qfixed2d pt = { x, y }; return pt; }
|
---|
140 | };
|
---|
141 |
|
---|
142 | QStrokerOps();
|
---|
143 | virtual ~QStrokerOps();
|
---|
144 |
|
---|
145 | void setMoveToHook(qStrokerMoveToHook moveToHook) { m_moveTo = moveToHook; }
|
---|
146 | void setLineToHook(qStrokerLineToHook lineToHook) { m_lineTo = lineToHook; }
|
---|
147 | void setCubicToHook(qStrokerCubicToHook cubicToHook) { m_cubicTo = cubicToHook; }
|
---|
148 |
|
---|
149 | virtual void begin(void *customData);
|
---|
150 | virtual void end();
|
---|
151 |
|
---|
152 | inline void moveTo(qfixed x, qfixed y);
|
---|
153 | inline void lineTo(qfixed x, qfixed y);
|
---|
154 | inline void cubicTo(qfixed x1, qfixed y1, qfixed x2, qfixed y2, qfixed ex, qfixed ey);
|
---|
155 |
|
---|
156 | void strokePath(const QPainterPath &path, void *data, const QTransform &matrix);
|
---|
157 | void strokePolygon(const QPointF *points, int pointCount, bool implicit_close,
|
---|
158 | void *data, const QTransform &matrix);
|
---|
159 | void strokeEllipse(const QRectF &ellipse, void *data, const QTransform &matrix);
|
---|
160 |
|
---|
161 | QRectF clipRect() const { return m_clip_rect; }
|
---|
162 | void setClipRect(const QRectF &clip) { m_clip_rect = clip; }
|
---|
163 |
|
---|
164 | protected:
|
---|
165 | inline void emitMoveTo(qfixed x, qfixed y);
|
---|
166 | inline void emitLineTo(qfixed x, qfixed y);
|
---|
167 | inline void emitCubicTo(qfixed c1x, qfixed c1y, qfixed c2x, qfixed c2y, qfixed ex, qfixed ey);
|
---|
168 |
|
---|
169 | virtual void processCurrentSubpath() = 0;
|
---|
170 | QDataBuffer<Element> m_elements;
|
---|
171 |
|
---|
172 | QRectF m_clip_rect;
|
---|
173 |
|
---|
174 | private:
|
---|
175 | void *m_customData;
|
---|
176 | qStrokerMoveToHook m_moveTo;
|
---|
177 | qStrokerLineToHook m_lineTo;
|
---|
178 | qStrokerCubicToHook m_cubicTo;
|
---|
179 |
|
---|
180 | };
|
---|
181 |
|
---|
182 | class Q_GUI_EXPORT QStroker : public QStrokerOps
|
---|
183 | {
|
---|
184 | public:
|
---|
185 |
|
---|
186 | enum LineJoinMode {
|
---|
187 | FlatJoin,
|
---|
188 | SquareJoin,
|
---|
189 | MiterJoin,
|
---|
190 | RoundJoin,
|
---|
191 | RoundCap,
|
---|
192 | SvgMiterJoin
|
---|
193 | };
|
---|
194 |
|
---|
195 | QStroker();
|
---|
196 | ~QStroker();
|
---|
197 |
|
---|
198 | void setStrokeWidth(qfixed width) { m_strokeWidth = width; }
|
---|
199 | qfixed strokeWidth() const { return m_strokeWidth; }
|
---|
200 |
|
---|
201 | void setCapStyle(Qt::PenCapStyle capStyle) { m_capStyle = joinModeForCap(capStyle); }
|
---|
202 | Qt::PenCapStyle capStyle() const { return capForJoinMode(m_capStyle); }
|
---|
203 | LineJoinMode capStyleMode() const { return m_capStyle; }
|
---|
204 |
|
---|
205 | void setJoinStyle(Qt::PenJoinStyle style) { m_joinStyle = joinModeForJoin(style); }
|
---|
206 | Qt::PenJoinStyle joinStyle() const { return joinForJoinMode(m_joinStyle); }
|
---|
207 | LineJoinMode joinStyleMode() const { return m_joinStyle; }
|
---|
208 |
|
---|
209 | void setMiterLimit(qfixed length) { m_miterLimit = length; }
|
---|
210 | qfixed miterLimit() const { return m_miterLimit; }
|
---|
211 |
|
---|
212 | void setCurveThreshold(qfixed threshold) { m_curveThreshold = threshold; }
|
---|
213 | qfixed curveThreshold() const { return m_curveThreshold; }
|
---|
214 |
|
---|
215 | void joinPoints(qfixed x, qfixed y, const QLineF &nextLine, LineJoinMode join);
|
---|
216 | inline void emitMoveTo(qfixed x, qfixed y);
|
---|
217 | inline void emitLineTo(qfixed x, qfixed y);
|
---|
218 | inline void emitCubicTo(qfixed c1x, qfixed c1y, qfixed c2x, qfixed c2y, qfixed ex, qfixed ey);
|
---|
219 |
|
---|
220 | protected:
|
---|
221 | static Qt::PenCapStyle capForJoinMode(LineJoinMode mode);
|
---|
222 | static LineJoinMode joinModeForCap(Qt::PenCapStyle);
|
---|
223 |
|
---|
224 | static Qt::PenJoinStyle joinForJoinMode(LineJoinMode mode);
|
---|
225 | static LineJoinMode joinModeForJoin(Qt::PenJoinStyle joinStyle);
|
---|
226 |
|
---|
227 | virtual void processCurrentSubpath();
|
---|
228 |
|
---|
229 | qfixed m_strokeWidth;
|
---|
230 | qfixed m_miterLimit;
|
---|
231 | qfixed m_curveThreshold;
|
---|
232 |
|
---|
233 | LineJoinMode m_capStyle;
|
---|
234 | LineJoinMode m_joinStyle;
|
---|
235 |
|
---|
236 | qfixed m_back1X;
|
---|
237 | qfixed m_back1Y;
|
---|
238 |
|
---|
239 | qfixed m_back2X;
|
---|
240 | qfixed m_back2Y;
|
---|
241 | };
|
---|
242 |
|
---|
243 | class Q_GUI_EXPORT QDashStroker : public QStrokerOps
|
---|
244 | {
|
---|
245 | public:
|
---|
246 | QDashStroker(QStroker *stroker);
|
---|
247 |
|
---|
248 | QStroker *stroker() const { return m_stroker; }
|
---|
249 |
|
---|
250 | static QVector<qfixed> patternForStyle(Qt::PenStyle style);
|
---|
251 |
|
---|
252 | void setDashPattern(const QVector<qfixed> &dashPattern) { m_dashPattern = dashPattern; }
|
---|
253 | QVector<qfixed> dashPattern() const { return m_dashPattern; }
|
---|
254 |
|
---|
255 | void setDashOffset(qreal offset) { m_dashOffset = offset; }
|
---|
256 | qreal dashOffset() const { return m_dashOffset; }
|
---|
257 |
|
---|
258 | virtual void begin(void *data);
|
---|
259 | virtual void end();
|
---|
260 |
|
---|
261 | protected:
|
---|
262 | virtual void processCurrentSubpath();
|
---|
263 |
|
---|
264 | QStroker *m_stroker;
|
---|
265 | QVector<qfixed> m_dashPattern;
|
---|
266 | qreal m_dashOffset;
|
---|
267 | };
|
---|
268 |
|
---|
269 |
|
---|
270 | /*******************************************************************************
|
---|
271 | * QStrokerOps inline membmers
|
---|
272 | */
|
---|
273 |
|
---|
274 | inline void QStrokerOps::emitMoveTo(qfixed x, qfixed y)
|
---|
275 | {
|
---|
276 | Q_ASSERT(m_moveTo);
|
---|
277 | m_moveTo(x, y, m_customData);
|
---|
278 | }
|
---|
279 |
|
---|
280 | inline void QStrokerOps::emitLineTo(qfixed x, qfixed y)
|
---|
281 | {
|
---|
282 | Q_ASSERT(m_lineTo);
|
---|
283 | m_lineTo(x, y, m_customData);
|
---|
284 | }
|
---|
285 |
|
---|
286 | inline void QStrokerOps::emitCubicTo(qfixed c1x, qfixed c1y, qfixed c2x, qfixed c2y, qfixed ex, qfixed ey)
|
---|
287 | {
|
---|
288 | Q_ASSERT(m_cubicTo);
|
---|
289 | m_cubicTo(c1x, c1y, c2x, c2y, ex, ey, m_customData);
|
---|
290 | }
|
---|
291 |
|
---|
292 | inline void QStrokerOps::moveTo(qfixed x, qfixed y)
|
---|
293 | {
|
---|
294 | if (m_elements.size()>1)
|
---|
295 | processCurrentSubpath();
|
---|
296 | m_elements.reset();
|
---|
297 | Element e = { QPainterPath::MoveToElement, x, y };
|
---|
298 | m_elements.add(e);
|
---|
299 | }
|
---|
300 |
|
---|
301 | inline void QStrokerOps::lineTo(qfixed x, qfixed y)
|
---|
302 | {
|
---|
303 | Element e = { QPainterPath::LineToElement, x, y };
|
---|
304 | m_elements.add(e);
|
---|
305 | }
|
---|
306 |
|
---|
307 | inline void QStrokerOps::cubicTo(qfixed x1, qfixed y1, qfixed x2, qfixed y2, qfixed ex, qfixed ey)
|
---|
308 | {
|
---|
309 | Element c1 = { QPainterPath::CurveToElement, x1, y1 };
|
---|
310 | Element c2 = { QPainterPath::CurveToDataElement, x2, y2 };
|
---|
311 | Element e = { QPainterPath::CurveToDataElement, ex, ey };
|
---|
312 | m_elements.add(c1);
|
---|
313 | m_elements.add(c2);
|
---|
314 | m_elements.add(e);
|
---|
315 | }
|
---|
316 |
|
---|
317 | /*******************************************************************************
|
---|
318 | * QStroker inline members
|
---|
319 | */
|
---|
320 | inline void QStroker::emitMoveTo(qfixed x, qfixed y)
|
---|
321 | {
|
---|
322 | m_back2X = m_back1X;
|
---|
323 | m_back2Y = m_back1Y;
|
---|
324 | m_back1X = x;
|
---|
325 | m_back1Y = y;
|
---|
326 | QStrokerOps::emitMoveTo(x, y);
|
---|
327 | }
|
---|
328 |
|
---|
329 | inline void QStroker::emitLineTo(qfixed x, qfixed y)
|
---|
330 | {
|
---|
331 | m_back2X = m_back1X;
|
---|
332 | m_back2Y = m_back1Y;
|
---|
333 | m_back1X = x;
|
---|
334 | m_back1Y = y;
|
---|
335 | QStrokerOps::emitLineTo(x, y);
|
---|
336 | }
|
---|
337 |
|
---|
338 | inline void QStroker::emitCubicTo(qfixed c1x, qfixed c1y,
|
---|
339 | qfixed c2x, qfixed c2y,
|
---|
340 | qfixed ex, qfixed ey)
|
---|
341 | {
|
---|
342 | if (c2x == ex && c2y == ey) {
|
---|
343 | if (c1x == ex && c1y == ey) {
|
---|
344 | m_back2X = m_back1X;
|
---|
345 | m_back2Y = m_back1Y;
|
---|
346 | } else {
|
---|
347 | m_back2X = c1x;
|
---|
348 | m_back2Y = c1y;
|
---|
349 | }
|
---|
350 | } else {
|
---|
351 | m_back2X = c2x;
|
---|
352 | m_back2Y = c2y;
|
---|
353 | }
|
---|
354 | m_back1X = ex;
|
---|
355 | m_back1Y = ey;
|
---|
356 | QStrokerOps::emitCubicTo(c1x, c1y, c2x, c2y, ex, ey);
|
---|
357 | }
|
---|
358 |
|
---|
359 | /*******************************************************************************
|
---|
360 | * QDashStroker inline members
|
---|
361 | */
|
---|
362 | inline void QDashStroker::begin(void *data)
|
---|
363 | {
|
---|
364 | Q_ASSERT(m_stroker);
|
---|
365 | m_stroker->begin(data);
|
---|
366 | QStrokerOps::begin(data);
|
---|
367 | }
|
---|
368 |
|
---|
369 | inline void QDashStroker::end()
|
---|
370 | {
|
---|
371 | Q_ASSERT(m_stroker);
|
---|
372 | QStrokerOps::end();
|
---|
373 | m_stroker->end();
|
---|
374 | }
|
---|
375 |
|
---|
376 | QT_END_NAMESPACE
|
---|
377 |
|
---|
378 | #endif // QSTROKER_P_H
|
---|