1/*
2 * Copyright (C) 2006, 2007, 2009 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#ifndef CanvasRenderingContext2D_h
27#define CanvasRenderingContext2D_h
28
29#include "AffineTransform.h"
30#include "CanvasRenderingContext.h"
31#include "Color.h"
32#include "FloatSize.h"
33#include "Font.h"
34#include "GraphicsTypes.h"
35#include "Path.h"
36#include "PlatformString.h"
37
38#include <wtf/Vector.h>
39
40#if USE(CG)
41#include <ApplicationServices/ApplicationServices.h>
42#endif
43
44#if USE(ACCELERATED_COMPOSITING)
45#include "GraphicsLayer.h"
46#endif
47
48namespace WebCore {
49
50class CanvasGradient;
51class CanvasPattern;
52class CanvasStyle;
53class FloatRect;
54class GraphicsContext;
55class HTMLCanvasElement;
56class HTMLImageElement;
57class HTMLVideoElement;
58class ImageData;
59class TextMetrics;
60
61#if ENABLE(ACCELERATED_2D_CANVAS)
62class DrawingBuffer;
63class SharedGraphicsContext3D;
64#endif
65
66typedef int ExceptionCode;
67
68class CanvasRenderingContext2D : public CanvasRenderingContext {
69public:
70    CanvasRenderingContext2D(HTMLCanvasElement*, bool usesCSSCompatibilityParseMode, bool usesDashboardCompatibilityMode);
71
72    virtual ~CanvasRenderingContext2D();
73
74    virtual bool is2d() const { return true; }
75    virtual bool isAccelerated() const;
76    virtual bool paintsIntoCanvasBuffer() const;
77
78    CanvasStyle* strokeStyle() const;
79    void setStrokeStyle(PassRefPtr<CanvasStyle>);
80
81    CanvasStyle* fillStyle() const;
82    void setFillStyle(PassRefPtr<CanvasStyle>);
83
84    float lineWidth() const;
85    void setLineWidth(float);
86
87    String lineCap() const;
88    void setLineCap(const String&);
89
90    String lineJoin() const;
91    void setLineJoin(const String&);
92
93    float miterLimit() const;
94    void setMiterLimit(float);
95
96    float shadowOffsetX() const;
97    void setShadowOffsetX(float);
98
99    float shadowOffsetY() const;
100    void setShadowOffsetY(float);
101
102    float shadowBlur() const;
103    void setShadowBlur(float);
104
105    String shadowColor() const;
106    void setShadowColor(const String&);
107
108    float globalAlpha() const;
109    void setGlobalAlpha(float);
110
111    String globalCompositeOperation() const;
112    void setGlobalCompositeOperation(const String&);
113
114    void save();
115    void restore();
116    void setAllAttributesToDefault();
117
118    void scale(float sx, float sy);
119    void rotate(float angleInRadians);
120    void translate(float tx, float ty);
121    void transform(float m11, float m12, float m21, float m22, float dx, float dy);
122    void setTransform(float m11, float m12, float m21, float m22, float dx, float dy);
123
124    void setStrokeColor(const String& color);
125    void setStrokeColor(float grayLevel);
126    void setStrokeColor(const String& color, float alpha);
127    void setStrokeColor(float grayLevel, float alpha);
128    void setStrokeColor(float r, float g, float b, float a);
129    void setStrokeColor(float c, float m, float y, float k, float a);
130
131    void setFillColor(const String& color);
132    void setFillColor(float grayLevel);
133    void setFillColor(const String& color, float alpha);
134    void setFillColor(float grayLevel, float alpha);
135    void setFillColor(float r, float g, float b, float a);
136    void setFillColor(float c, float m, float y, float k, float a);
137
138    void beginPath();
139    void closePath();
140
141    void moveTo(float x, float y);
142    void lineTo(float x, float y);
143    void quadraticCurveTo(float cpx, float cpy, float x, float y);
144    void bezierCurveTo(float cp1x, float cp1y, float cp2x, float cp2y, float x, float y);
145    void arcTo(float x0, float y0, float x1, float y1, float radius, ExceptionCode&);
146    void arc(float x, float y, float r, float sa, float ea, bool clockwise, ExceptionCode&);
147    void rect(float x, float y, float width, float height);
148
149    void fill();
150    void stroke();
151    void clip();
152
153    bool isPointInPath(const float x, const float y);
154
155    void clearRect(float x, float y, float width, float height);
156    void fillRect(float x, float y, float width, float height);
157    void strokeRect(float x, float y, float width, float height);
158    void strokeRect(float x, float y, float width, float height, float lineWidth);
159
160    void setShadow(float width, float height, float blur);
161    void setShadow(float width, float height, float blur, const String& color);
162    void setShadow(float width, float height, float blur, float grayLevel);
163    void setShadow(float width, float height, float blur, const String& color, float alpha);
164    void setShadow(float width, float height, float blur, float grayLevel, float alpha);
165    void setShadow(float width, float height, float blur, float r, float g, float b, float a);
166    void setShadow(float width, float height, float blur, float c, float m, float y, float k, float a);
167
168    void clearShadow();
169
170    void drawImage(HTMLImageElement*, float x, float y, ExceptionCode&);
171    void drawImage(HTMLImageElement*, float x, float y, float width, float height, ExceptionCode&);
172    void drawImage(HTMLImageElement*, float sx, float sy, float sw, float sh, float dx, float dy, float dw, float dh, ExceptionCode&);
173    void drawImage(HTMLImageElement*, const FloatRect& srcRect, const FloatRect& dstRect, ExceptionCode&);
174    void drawImage(HTMLCanvasElement*, float x, float y, ExceptionCode&);
175    void drawImage(HTMLCanvasElement*, float x, float y, float width, float height, ExceptionCode&);
176    void drawImage(HTMLCanvasElement*, float sx, float sy, float sw, float sh, float dx, float dy, float dw, float dh, ExceptionCode&);
177    void drawImage(HTMLCanvasElement*, const FloatRect& srcRect, const FloatRect& dstRect, ExceptionCode&);
178    void drawImage(HTMLImageElement*, const FloatRect& srcRect, const FloatRect& dstRect, const CompositeOperator&, ExceptionCode&);
179#if ENABLE(VIDEO)
180    void drawImage(HTMLVideoElement*, float x, float y, ExceptionCode&);
181    void drawImage(HTMLVideoElement*, float x, float y, float width, float height, ExceptionCode&);
182    void drawImage(HTMLVideoElement*, float sx, float sy, float sw, float sh, float dx, float dy, float dw, float dh, ExceptionCode&);
183    void drawImage(HTMLVideoElement*, const FloatRect& srcRect, const FloatRect& dstRect, ExceptionCode&);
184#endif
185
186    void drawImageFromRect(HTMLImageElement*, float sx, float sy, float sw, float sh,
187        float dx, float dy, float dw, float dh, const String& compositeOperation);
188
189    void setAlpha(float);
190
191    void setCompositeOperation(const String&);
192
193    PassRefPtr<CanvasGradient> createLinearGradient(float x0, float y0, float x1, float y1, ExceptionCode&);
194    PassRefPtr<CanvasGradient> createRadialGradient(float x0, float y0, float r0, float x1, float y1, float r1, ExceptionCode&);
195    PassRefPtr<CanvasPattern> createPattern(HTMLImageElement*, const String& repetitionType, ExceptionCode&);
196    PassRefPtr<CanvasPattern> createPattern(HTMLCanvasElement*, const String& repetitionType, ExceptionCode&);
197
198    PassRefPtr<ImageData> createImageData(PassRefPtr<ImageData>, ExceptionCode&) const;
199    PassRefPtr<ImageData> createImageData(float width, float height, ExceptionCode&) const;
200    PassRefPtr<ImageData> getImageData(float sx, float sy, float sw, float sh, ExceptionCode&) const;
201    void putImageData(ImageData*, float dx, float dy, ExceptionCode&);
202    void putImageData(ImageData*, float dx, float dy, float dirtyX, float dirtyY, float dirtyWidth, float dirtyHeight, ExceptionCode&);
203
204    void reset();
205
206    String font() const;
207    void setFont(const String&);
208
209    String textAlign() const;
210    void setTextAlign(const String&);
211
212    String textBaseline() const;
213    void setTextBaseline(const String&);
214
215    void fillText(const String& text, float x, float y);
216    void fillText(const String& text, float x, float y, float maxWidth);
217    void strokeText(const String& text, float x, float y);
218    void strokeText(const String& text, float x, float y, float maxWidth);
219    PassRefPtr<TextMetrics> measureText(const String& text);
220
221    LineCap getLineCap() const { return state().m_lineCap; }
222    LineJoin getLineJoin() const { return state().m_lineJoin; }
223
224    virtual void paintRenderingResultsToCanvas();
225
226#if ENABLE(ACCELERATED_2D_CANVAS) && USE(ACCELERATED_COMPOSITING)
227    virtual PlatformLayer* platformLayer() const;
228#endif
229
230private:
231    struct State : FontSelectorClient {
232        State();
233        virtual ~State();
234
235        State(const State&);
236        State& operator=(const State&);
237
238        virtual void fontsNeedUpdate(FontSelector*);
239
240        String m_unparsedStrokeColor;
241        String m_unparsedFillColor;
242        RefPtr<CanvasStyle> m_strokeStyle;
243        RefPtr<CanvasStyle> m_fillStyle;
244        float m_lineWidth;
245        LineCap m_lineCap;
246        LineJoin m_lineJoin;
247        float m_miterLimit;
248        FloatSize m_shadowOffset;
249        float m_shadowBlur;
250        RGBA32 m_shadowColor;
251        float m_globalAlpha;
252        CompositeOperator m_globalComposite;
253        AffineTransform m_transform;
254        bool m_invertibleCTM;
255
256        // Text state.
257        TextAlign m_textAlign;
258        TextBaseline m_textBaseline;
259
260        String m_unparsedFont;
261        Font m_font;
262        bool m_realizedFont;
263    };
264    Path m_path;
265
266    State& state() { return m_stateStack.last(); }
267    const State& state() const { return m_stateStack.last(); }
268
269    void applyShadow();
270
271    enum CanvasDidDrawOption {
272        CanvasDidDrawApplyNone = 0,
273        CanvasDidDrawApplyTransform = 1,
274        CanvasDidDrawApplyShadow = 1 << 1,
275        CanvasDidDrawApplyClip = 1 << 2,
276        CanvasDidDrawApplyAll = 0xffffffff
277    };
278
279    void didDraw(const FloatRect&, unsigned options = CanvasDidDrawApplyAll);
280
281    GraphicsContext* drawingContext() const;
282
283    void applyStrokePattern();
284    void applyFillPattern();
285
286    void drawTextInternal(const String& text, float x, float y, bool fill, float maxWidth = 0, bool useMaxWidth = false);
287
288    const Font& accessFont();
289
290#if ENABLE(DASHBOARD_SUPPORT)
291    void clearPathForDashboardBackwardCompatibilityMode();
292#endif
293
294    void prepareGradientForDashboard(CanvasGradient* gradient) const;
295
296    Vector<State, 1> m_stateStack;
297    bool m_usesCSSCompatibilityParseMode;
298#if ENABLE(DASHBOARD_SUPPORT)
299    bool m_usesDashboardCompatibilityMode;
300#endif
301
302#if ENABLE(ACCELERATED_2D_CANVAS)
303    RefPtr<DrawingBuffer> m_drawingBuffer;
304    RefPtr<SharedGraphicsContext3D> m_context3D;
305#endif
306};
307
308} // namespace WebCore
309
310#endif
311