initial import
[vuplus_webkit] / Source / WebCore / html / canvas / CanvasRenderingContext2D.h
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 #include <wtf/Vector.h>
38
39 #if USE(ACCELERATED_COMPOSITING)
40 #include "GraphicsLayer.h"
41 #endif
42
43 namespace WebCore {
44
45 class CanvasGradient;
46 class CanvasPattern;
47 class CanvasStyle;
48 class FloatRect;
49 class GraphicsContext;
50 class HTMLCanvasElement;
51 class HTMLImageElement;
52 class HTMLVideoElement;
53 class ImageData;
54 class TextMetrics;
55
56 typedef int ExceptionCode;
57
58 class CanvasRenderingContext2D : public CanvasRenderingContext {
59 public:
60     static PassOwnPtr<CanvasRenderingContext2D> create(HTMLCanvasElement* canvas, bool usesCSSCompatibilityParseMode, bool usesDashboardCompatibilityMode)
61     {
62         return adoptPtr(new CanvasRenderingContext2D(canvas, usesCSSCompatibilityParseMode, usesDashboardCompatibilityMode));
63     }
64     virtual ~CanvasRenderingContext2D();
65
66     virtual bool is2d() const { return true; }
67     virtual bool isAccelerated() const;
68     virtual bool paintsIntoCanvasBuffer() const;
69
70     CanvasStyle* strokeStyle() const;
71     void setStrokeStyle(PassRefPtr<CanvasStyle>);
72
73     CanvasStyle* fillStyle() const;
74     void setFillStyle(PassRefPtr<CanvasStyle>);
75
76     float lineWidth() const;
77     void setLineWidth(float);
78
79     String lineCap() const;
80     void setLineCap(const String&);
81
82     String lineJoin() const;
83     void setLineJoin(const String&);
84
85     float miterLimit() const;
86     void setMiterLimit(float);
87
88     float shadowOffsetX() const;
89     void setShadowOffsetX(float);
90
91     float shadowOffsetY() const;
92     void setShadowOffsetY(float);
93
94     float shadowBlur() const;
95     void setShadowBlur(float);
96
97     String shadowColor() const;
98     void setShadowColor(const String&);
99
100     float globalAlpha() const;
101     void setGlobalAlpha(float);
102
103     String globalCompositeOperation() const;
104     void setGlobalCompositeOperation(const String&);
105
106     void save();
107     void restore();
108     void setAllAttributesToDefault();
109
110     void scale(float sx, float sy);
111     void rotate(float angleInRadians);
112     void translate(float tx, float ty);
113     void transform(float m11, float m12, float m21, float m22, float dx, float dy);
114     void setTransform(float m11, float m12, float m21, float m22, float dx, float dy);
115
116     void setStrokeColor(const String& color);
117     void setStrokeColor(float grayLevel);
118     void setStrokeColor(const String& color, float alpha);
119     void setStrokeColor(float grayLevel, float alpha);
120     void setStrokeColor(float r, float g, float b, float a);
121     void setStrokeColor(float c, float m, float y, float k, float a);
122
123     void setFillColor(const String& color);
124     void setFillColor(float grayLevel);
125     void setFillColor(const String& color, float alpha);
126     void setFillColor(float grayLevel, float alpha);
127     void setFillColor(float r, float g, float b, float a);
128     void setFillColor(float c, float m, float y, float k, float a);
129
130     void beginPath();
131     void closePath();
132
133     void moveTo(float x, float y);
134     void lineTo(float x, float y);
135     void quadraticCurveTo(float cpx, float cpy, float x, float y);
136     void bezierCurveTo(float cp1x, float cp1y, float cp2x, float cp2y, float x, float y);
137     void arcTo(float x0, float y0, float x1, float y1, float radius, ExceptionCode&);
138     void arc(float x, float y, float r, float sa, float ea, bool clockwise, ExceptionCode&);
139     void rect(float x, float y, float width, float height);
140
141     void fill();
142     void stroke();
143     void clip();
144
145     bool isPointInPath(const float x, const float y);
146
147     void clearRect(float x, float y, float width, float height);
148     void fillRect(float x, float y, float width, float height);
149     void strokeRect(float x, float y, float width, float height);
150     void strokeRect(float x, float y, float width, float height, float lineWidth);
151
152     void setShadow(float width, float height, float blur);
153     void setShadow(float width, float height, float blur, const String& color);
154     void setShadow(float width, float height, float blur, float grayLevel);
155     void setShadow(float width, float height, float blur, const String& color, float alpha);
156     void setShadow(float width, float height, float blur, float grayLevel, float alpha);
157     void setShadow(float width, float height, float blur, float r, float g, float b, float a);
158     void setShadow(float width, float height, float blur, float c, float m, float y, float k, float a);
159
160     void clearShadow();
161
162     void drawImage(HTMLImageElement*, float x, float y, ExceptionCode&);
163     void drawImage(HTMLImageElement*, float x, float y, float width, float height, ExceptionCode&);
164     void drawImage(HTMLImageElement*, float sx, float sy, float sw, float sh, float dx, float dy, float dw, float dh, ExceptionCode&);
165     void drawImage(HTMLImageElement*, const FloatRect& srcRect, const FloatRect& dstRect, ExceptionCode&);
166     void drawImage(HTMLCanvasElement*, float x, float y, ExceptionCode&);
167     void drawImage(HTMLCanvasElement*, float x, float y, float width, float height, ExceptionCode&);
168     void drawImage(HTMLCanvasElement*, float sx, float sy, float sw, float sh, float dx, float dy, float dw, float dh, ExceptionCode&);
169     void drawImage(HTMLCanvasElement*, const FloatRect& srcRect, const FloatRect& dstRect, ExceptionCode&);
170     void drawImage(HTMLImageElement*, const FloatRect& srcRect, const FloatRect& dstRect, const CompositeOperator&, ExceptionCode&);
171 #if ENABLE(VIDEO)
172     void drawImage(HTMLVideoElement*, float x, float y, ExceptionCode&);
173     void drawImage(HTMLVideoElement*, float x, float y, float width, float height, ExceptionCode&);
174     void drawImage(HTMLVideoElement*, float sx, float sy, float sw, float sh, float dx, float dy, float dw, float dh, ExceptionCode&);
175     void drawImage(HTMLVideoElement*, const FloatRect& srcRect, const FloatRect& dstRect, ExceptionCode&);
176 #endif
177
178     void drawImageFromRect(HTMLImageElement*, float sx = 0, float sy = 0, float sw = 0, float sh = 0,
179                            float dx = 0, float dy = 0, float dw = 0, float dh = 0, const String& compositeOperation = emptyString());
180
181     void setAlpha(float);
182
183     void setCompositeOperation(const String&);
184
185     PassRefPtr<CanvasGradient> createLinearGradient(float x0, float y0, float x1, float y1, ExceptionCode&);
186     PassRefPtr<CanvasGradient> createRadialGradient(float x0, float y0, float r0, float x1, float y1, float r1, ExceptionCode&);
187     PassRefPtr<CanvasPattern> createPattern(HTMLImageElement*, const String& repetitionType, ExceptionCode&);
188     PassRefPtr<CanvasPattern> createPattern(HTMLCanvasElement*, const String& repetitionType, ExceptionCode&);
189
190     PassRefPtr<ImageData> createImageData(PassRefPtr<ImageData>, ExceptionCode&) const;
191     PassRefPtr<ImageData> createImageData(float width, float height, ExceptionCode&) const;
192     PassRefPtr<ImageData> getImageData(float sx, float sy, float sw, float sh, ExceptionCode&) const;
193     void putImageData(ImageData*, float dx, float dy, ExceptionCode&);
194     void putImageData(ImageData*, float dx, float dy, float dirtyX, float dirtyY, float dirtyWidth, float dirtyHeight, ExceptionCode&);
195
196     void reset();
197
198     String font() const;
199     void setFont(const String&);
200
201     String textAlign() const;
202     void setTextAlign(const String&);
203
204     String textBaseline() const;
205     void setTextBaseline(const String&);
206
207     void fillText(const String& text, float x, float y);
208     void fillText(const String& text, float x, float y, float maxWidth);
209     void strokeText(const String& text, float x, float y);
210     void strokeText(const String& text, float x, float y, float maxWidth);
211     PassRefPtr<TextMetrics> measureText(const String& text);
212
213     LineCap getLineCap() const { return state().m_lineCap; }
214     LineJoin getLineJoin() const { return state().m_lineJoin; }
215
216 #if ENABLE(ACCELERATED_2D_CANVAS) && USE(ACCELERATED_COMPOSITING)
217     virtual PlatformLayer* platformLayer() const;
218 #endif
219
220 private:
221     struct State : FontSelectorClient {
222         State();
223         virtual ~State();
224
225         State(const State&);
226         State& operator=(const State&);
227
228         virtual void fontsNeedUpdate(FontSelector*);
229
230         String m_unparsedStrokeColor;
231         String m_unparsedFillColor;
232         RefPtr<CanvasStyle> m_strokeStyle;
233         RefPtr<CanvasStyle> m_fillStyle;
234         float m_lineWidth;
235         LineCap m_lineCap;
236         LineJoin m_lineJoin;
237         float m_miterLimit;
238         FloatSize m_shadowOffset;
239         float m_shadowBlur;
240         RGBA32 m_shadowColor;
241         float m_globalAlpha;
242         CompositeOperator m_globalComposite;
243         AffineTransform m_transform;
244         bool m_invertibleCTM;
245
246         // Text state.
247         TextAlign m_textAlign;
248         TextBaseline m_textBaseline;
249
250         String m_unparsedFont;
251         Font m_font;
252         bool m_realizedFont;
253     };
254
255     enum CanvasDidDrawOption {
256         CanvasDidDrawApplyNone = 0,
257         CanvasDidDrawApplyTransform = 1,
258         CanvasDidDrawApplyShadow = 1 << 1,
259         CanvasDidDrawApplyClip = 1 << 2,
260         CanvasDidDrawApplyAll = 0xffffffff
261     };
262
263     CanvasRenderingContext2D(HTMLCanvasElement*, bool usesCSSCompatibilityParseMode, bool usesDashboardCompatibilityMode);
264
265     Path m_path;
266
267     State& state() { return m_stateStack.last(); }
268     const State& state() const { return m_stateStack.last(); }
269
270     void applyShadow();
271
272     void didDraw(const FloatRect&, unsigned options = CanvasDidDrawApplyAll);
273
274     GraphicsContext* drawingContext() const;
275
276     void unwindStateStack();
277
278     void applyStrokePattern();
279     void applyFillPattern();
280
281     void drawTextInternal(const String& text, float x, float y, bool fill, float maxWidth = 0, bool useMaxWidth = false);
282
283     const Font& accessFont();
284
285 #if ENABLE(DASHBOARD_SUPPORT)
286     void clearPathForDashboardBackwardCompatibilityMode();
287 #endif
288
289     void clearCanvas();
290     Path transformAreaToDevice(const Path&) const;
291     Path transformAreaToDevice(const FloatRect&) const;
292     bool shouldDisplayTransparencyElsewhere() const;
293     template<class T> void fillAndDisplayTransparencyElsewhere(const T& area);
294
295     void prepareGradientForDashboard(CanvasGradient* gradient) const;
296
297     Vector<State, 1> m_stateStack;
298     bool m_usesCSSCompatibilityParseMode;
299 #if ENABLE(DASHBOARD_SUPPORT)
300     bool m_usesDashboardCompatibilityMode;
301 #endif
302 };
303
304 } // namespace WebCore
305
306 #endif