Make QRegion not need to be friends with QVector
[profile/ivi/qtbase.git] / src / gui / painting / qoutlinemapper_p.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the QtGui module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
16 **
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
20 **
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
28 **
29 ** Other Usage
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #ifndef QOUTLINEMAPPER_P_H
43 #define QOUTLINEMAPPER_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 <QtCore/qrect.h>
57
58 #include <QtGui/qtransform.h>
59 #include <QtGui/qpainterpath.h>
60
61 #define QT_FT_BEGIN_HEADER
62 #define QT_FT_END_HEADER
63
64 #include <private/qrasterdefs_p.h>
65 #include <private/qdatabuffer_p.h>
66 #include "qpaintengineex_p.h"
67
68 QT_BEGIN_NAMESPACE
69
70 // This limitations comes from qgrayraster.c. Any higher and
71 // rasterization of shapes will produce incorrect results.
72 const int QT_RASTER_COORD_LIMIT = 32767;
73
74 //#define QT_DEBUG_CONVERT
75
76 Q_GUI_EXPORT bool qt_scaleForTransform(const QTransform &transform, qreal *scale);
77
78 /********************************************************************************
79  * class QOutlineMapper
80  *
81  * Used to map between QPainterPath and the QT_FT_Outline structure used by the
82  * freetype scanconvertor.
83  *
84  * The outline mapper uses a path iterator to get points from the path,
85  * so that it is possible to transform the points as they are converted. The
86  * callback can be a noop, translate or full-fledged xform. (Tests indicated
87  * that using a C callback was low cost).
88  */
89 class QOutlineMapper
90 {
91 public:
92     QOutlineMapper() :
93         m_element_types(0),
94         m_elements(0),
95         m_points(0),
96         m_tags(0),
97         m_contours(0),
98         m_in_clip_elements(false),
99         m_round_coords(false)
100     {
101     }
102
103     /*!
104       Sets up the matrix to be used for conversion. This also
105       sets up the qt_path_iterator function that is used as a callback
106       to get points.
107     */
108     void setMatrix(const QTransform &m)
109     {
110         m_m11 = m.m11();
111         m_m12 = m.m12();
112         m_m13 = m.m13();
113         m_m21 = m.m21();
114         m_m22 = m.m22();
115         m_m23 = m.m23();
116         m_m33 = m.m33();
117         m_dx = m.dx();
118         m_dy = m.dy();
119         m_txop = m.type();
120
121         qreal scale;
122         qt_scaleForTransform(m, &scale);
123         m_curve_threshold = scale == 0 ? qreal(0.25) : (qreal(0.25) / scale);
124     }
125
126     void beginOutline(Qt::FillRule fillRule)
127     {
128 #ifdef QT_DEBUG_CONVERT
129         printf("QOutlineMapper::beginOutline rule=%d\n", fillRule);
130 #endif
131         m_valid = true;
132         m_elements.reset();
133         m_element_types.reset();
134         m_points.reset();
135         m_tags.reset();
136         m_contours.reset();
137         m_outline.flags = fillRule == Qt::WindingFill
138                           ? QT_FT_OUTLINE_NONE
139                           : QT_FT_OUTLINE_EVEN_ODD_FILL;
140         m_subpath_start = 0;
141     }
142
143     void endOutline();
144
145     void clipElements(const QPointF *points, const QPainterPath::ElementType *types, int count);
146
147     void convertElements(const QPointF *points, const QPainterPath::ElementType *types, int count);
148
149     inline void moveTo(const QPointF &pt) {
150 #ifdef QT_DEBUG_CONVERT
151         printf("QOutlineMapper::moveTo() (%f, %f)\n", pt.x(), pt.y());
152 #endif
153         closeSubpath();
154         m_subpath_start = m_elements.size();
155         m_elements << pt;
156         m_element_types << QPainterPath::MoveToElement;
157     }
158
159     inline void lineTo(const QPointF &pt) {
160 #ifdef QT_DEBUG_CONVERT
161         printf("QOutlineMapper::lineTo() (%f, %f)\n", pt.x(), pt.y());
162 #endif
163         m_elements.add(pt);
164         m_element_types << QPainterPath::LineToElement;
165     }
166
167     void curveTo(const QPointF &cp1, const QPointF &cp2, const QPointF &ep);
168
169     inline void closeSubpath() {
170         int element_count = m_elements.size();
171         if (element_count > 0) {
172             if (m_elements.at(element_count-1) != m_elements.at(m_subpath_start)) {
173 #ifdef QT_DEBUG_CONVERT
174                 printf(" - implicitly closing\n");
175 #endif
176                 // Put the object on the stack to avoid the odd case where
177                 // lineTo reallocs the databuffer and the QPointF & will
178                 // be invalidated.
179                 QPointF pt = m_elements.at(m_subpath_start);
180
181                 // only do lineTo if we have element_type array...
182                 if (m_element_types.size())
183                     lineTo(pt);
184                 else
185                     m_elements << pt;
186
187             }
188         }
189     }
190
191     QT_FT_Outline *outline() {
192         if (m_valid)
193             return &m_outline;
194         return 0;
195     }
196
197     QT_FT_Outline *convertPath(const QPainterPath &path);
198     QT_FT_Outline *convertPath(const QVectorPath &path);
199
200     void setCoordinateRounding(bool coordinateRounding) { m_round_coords = coordinateRounding; }
201
202     inline QPainterPath::ElementType *elementTypes() const { return m_element_types.size() == 0 ? 0 : m_element_types.data(); }
203
204 public:
205     QDataBuffer<QPainterPath::ElementType> m_element_types;
206     QDataBuffer<QPointF> m_elements;
207     QDataBuffer<QT_FT_Vector> m_points;
208     QDataBuffer<char> m_tags;
209     QDataBuffer<int> m_contours;
210
211     QRect m_clip_rect;
212     QRectF controlPointRect; // only valid after endOutline()
213
214     QT_FT_Outline m_outline;
215     uint m_txop;
216
217     int m_subpath_start;
218
219     // Matrix
220     qreal m_m11;
221     qreal m_m12;
222     qreal m_m13;
223     qreal m_m21;
224     qreal m_m22;
225     qreal m_m23;
226     qreal m_m33;
227     qreal m_dx;
228     qreal m_dy;
229
230     qreal m_curve_threshold;
231
232     bool m_valid;
233     bool m_in_clip_elements;
234
235 private:
236     bool m_round_coords;
237 };
238
239 QT_END_NAMESPACE
240
241 #endif // QOUTLINEMAPPER_P_H