1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtQml module of the Qt Toolkit.
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.
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.
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.
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.
40 ****************************************************************************/
42 #ifndef QQUICKCONTEXT2D_P_H
43 #define QQUICKCONTEXT2D_P_H
45 #include <QtQuick/qtquickglobal.h>
46 #include <QtQml/qqml.h>
47 #include <QtQml/qqmlcomponent.h>
48 #include <private/qquickcanvascontext_p.h>
49 #include <private/qquickcanvasitem_p.h>
50 #include <QtGui/qpainter.h>
51 #include <QtGui/qpainterpath.h>
52 #include <QtCore/qstring.h>
53 #include <QtCore/qstack.h>
54 #include <QtCore/qqueue.h>
55 #include <private/qv8engine_p.h>
59 //#define QQUICKCONTEXT2D_DEBUG //enable this for just DEBUG purpose!
61 #ifdef QQUICKCONTEXT2D_DEBUG
62 #include <QElapsedTimer>
69 class QQuickContext2DCommandBuffer;
70 class QQuickContext2DTexture;
75 class QQuickContext2D : public QQuickCanvasContext
78 enum TextBaseLineType { Alphabetic=0, Top, Middle, Bottom, Hanging};
79 enum TextAlignType { Start=0, End, Left, Right, Center};
91 GlobalCompositeOperation,
113 : strokeStyle(QColor("#000000"))
114 , fillStyle(QColor("#000000"))
115 , fillPatternRepeatX(false)
116 , fillPatternRepeatY(false)
117 , strokePatternRepeatX(false)
118 , strokePatternRepeatY(false)
119 , invertibleCTM(true)
120 , fillRule(Qt::WindingFill)
123 , lineCap(Qt::FlatCap)
124 , lineJoin(Qt::MiterJoin)
129 , shadowColor(qRgba(0, 0, 0, 0))
130 , globalCompositeOperation(QPainter::CompositionMode_SourceOver)
131 , font(QFont(QLatin1String("sans-serif"), 10))
132 , textAlign(QQuickContext2D::Start)
133 , textBaseline(QQuickContext2D::Alphabetic)
138 QPainterPath clipPath;
141 bool fillPatternRepeatX:1;
142 bool fillPatternRepeatY:1;
143 bool strokePatternRepeatX:1;
144 bool strokePatternRepeatY:1;
145 bool invertibleCTM:1;
146 Qt::FillRule fillRule;
149 Qt::PenCapStyle lineCap;
150 Qt::PenJoinStyle lineJoin;
156 QPainter::CompositionMode globalCompositeOperation;
158 QQuickContext2D::TextAlignType textAlign;
159 QQuickContext2D::TextBaseLineType textBaseline;
162 QQuickContext2D(QObject *parent = 0);
165 QStringList contextNames() const;
166 void init(QQuickCanvasItem *canvasItem, const QVariantMap &args);
167 void prepare(const QSize& canvasSize, const QSize& tileSize, const QRect& canvasWindow, const QRect& dirtyRect, bool smooth);
170 QSGDynamicTexture *texture() const;
171 QImage toImage(const QRectF& bounds);
173 v8::Handle<v8::Object> v8value() const;
174 void setV8Engine(QV8Engine *eng);
176 QQuickCanvasItem* canvas() const { return m_canvas; }
177 QQuickContext2DCommandBuffer* buffer() const { return m_buffer; }
178 QQuickContext2DCommandBuffer* nextBuffer();
180 bool bufferValid() const { return m_buffer != 0; }
188 void fillRect(qreal x, qreal y, qreal w, qreal h);
189 void strokeRect(qreal x, qreal y, qreal w, qreal h);
190 void clearRect(qreal x, qreal y, qreal w, qreal h);
191 void drawText(const QString& text, qreal x, qreal y, bool fill);
194 void scale(qreal x, qreal y);
195 void rotate(qreal angle);
196 void shear(qreal h, qreal v);
197 void translate(qreal x, qreal y);
198 void transform(qreal a, qreal b, qreal c, qreal d, qreal e, qreal f);
199 void setTransform(qreal a, qreal b, qreal c, qreal d, qreal e, qreal f);
204 void moveTo(qreal x, qreal y);
205 void lineTo(qreal x, qreal y);
206 void quadraticCurveTo(qreal cpx, qreal cpy, qreal x, qreal y);
207 void bezierCurveTo(qreal cp1x, qreal cp1y,
208 qreal cp2x, qreal cp2y, qreal x, qreal y);
209 void arcTo(qreal x1, qreal y1, qreal x2, qreal y2, qreal radius);
210 void rect(qreal x, qreal y, qreal w, qreal h);
211 void roundedRect(qreal x, qreal y,qreal w, qreal h, qreal xr, qreal yr);
212 void ellipse(qreal x, qreal y,qreal w, qreal h);
213 void text(const QString& str, qreal x, qreal y);
214 void arc(qreal x, qreal y, qreal radius,
215 qreal startAngle, qreal endAngle,
217 void addArcTo(const QPointF& p1, const QPointF& p2, float radius);
219 bool isPointInPath(qreal x, qreal y) const;
221 QPainterPath createTextGlyphs(qreal x, qreal y, const QString& text);
222 QImage createImage(const QUrl& url);
225 QStack<QQuickContext2D::State> m_stateStack;
226 QQuickCanvasItem* m_canvas;
227 QQuickContext2DCommandBuffer* m_buffer;
229 v8::Local<v8::Value> m_fillStyle;
230 v8::Local<v8::Value> m_strokeStyle;
231 v8::Handle<v8::Value> m_v8path;
232 QV8Engine *m_v8engine;
233 v8::Persistent<v8::Object> m_v8value;
234 QQuickContext2DTexture *m_texture;
235 QQuickCanvasItem::RenderTarget m_renderTarget;
236 QQuickCanvasItem::RenderStrategy m_renderStrategy;
237 QQueue<QQuickContext2DCommandBuffer*> m_bufferQueue;
238 QMutex m_bufferMutex;
243 QML_DECLARE_TYPE(QQuickContext2D)
247 #endif // QQUICKCONTEXT2D_P_H