2 * Copyright (C) 2006, 2007, 2009 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
26 #ifndef CanvasRenderingContext2D_h
27 #define CanvasRenderingContext2D_h
29 #include "AffineTransform.h"
30 #include "CanvasRenderingContext.h"
32 #include "FloatSize.h"
34 #include "GraphicsTypes.h"
36 #include "PlatformString.h"
37 #include <wtf/Vector.h>
39 #if USE(ACCELERATED_COMPOSITING)
40 #include "GraphicsLayer.h"
49 class GraphicsContext;
50 class HTMLCanvasElement;
51 class HTMLImageElement;
52 class HTMLVideoElement;
56 typedef int ExceptionCode;
58 class CanvasRenderingContext2D : public CanvasRenderingContext {
60 static PassOwnPtr<CanvasRenderingContext2D> create(HTMLCanvasElement* canvas, bool usesCSSCompatibilityParseMode, bool usesDashboardCompatibilityMode)
62 return adoptPtr(new CanvasRenderingContext2D(canvas, usesCSSCompatibilityParseMode, usesDashboardCompatibilityMode));
64 virtual ~CanvasRenderingContext2D();
66 virtual bool is2d() const { return true; }
67 virtual bool isAccelerated() const;
68 virtual bool paintsIntoCanvasBuffer() const;
70 CanvasStyle* strokeStyle() const;
71 void setStrokeStyle(PassRefPtr<CanvasStyle>);
73 CanvasStyle* fillStyle() const;
74 void setFillStyle(PassRefPtr<CanvasStyle>);
76 float lineWidth() const;
77 void setLineWidth(float);
79 String lineCap() const;
80 void setLineCap(const String&);
82 String lineJoin() const;
83 void setLineJoin(const String&);
85 float miterLimit() const;
86 void setMiterLimit(float);
88 float shadowOffsetX() const;
89 void setShadowOffsetX(float);
91 float shadowOffsetY() const;
92 void setShadowOffsetY(float);
94 float shadowBlur() const;
95 void setShadowBlur(float);
97 String shadowColor() const;
98 void setShadowColor(const String&);
100 float globalAlpha() const;
101 void setGlobalAlpha(float);
103 String globalCompositeOperation() const;
104 void setGlobalCompositeOperation(const String&);
108 void setAllAttributesToDefault();
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);
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);
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);
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);
145 bool isPointInPath(const float x, const float y);
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);
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);
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&);
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&);
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());
181 void setAlpha(float);
183 void setCompositeOperation(const String&);
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&);
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&);
199 void setFont(const String&);
201 String textAlign() const;
202 void setTextAlign(const String&);
204 String textBaseline() const;
205 void setTextBaseline(const String&);
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);
213 LineCap getLineCap() const { return state().m_lineCap; }
214 LineJoin getLineJoin() const { return state().m_lineJoin; }
216 #if ENABLE(ACCELERATED_2D_CANVAS) && USE(ACCELERATED_COMPOSITING)
217 virtual PlatformLayer* platformLayer() const;
221 struct State : FontSelectorClient {
226 State& operator=(const State&);
228 virtual void fontsNeedUpdate(FontSelector*);
230 String m_unparsedStrokeColor;
231 String m_unparsedFillColor;
232 RefPtr<CanvasStyle> m_strokeStyle;
233 RefPtr<CanvasStyle> m_fillStyle;
238 FloatSize m_shadowOffset;
240 RGBA32 m_shadowColor;
242 CompositeOperator m_globalComposite;
243 AffineTransform m_transform;
244 bool m_invertibleCTM;
247 TextAlign m_textAlign;
248 TextBaseline m_textBaseline;
250 String m_unparsedFont;
255 enum CanvasDidDrawOption {
256 CanvasDidDrawApplyNone = 0,
257 CanvasDidDrawApplyTransform = 1,
258 CanvasDidDrawApplyShadow = 1 << 1,
259 CanvasDidDrawApplyClip = 1 << 2,
260 CanvasDidDrawApplyAll = 0xffffffff
263 CanvasRenderingContext2D(HTMLCanvasElement*, bool usesCSSCompatibilityParseMode, bool usesDashboardCompatibilityMode);
267 State& state() { return m_stateStack.last(); }
268 const State& state() const { return m_stateStack.last(); }
272 void didDraw(const FloatRect&, unsigned options = CanvasDidDrawApplyAll);
274 GraphicsContext* drawingContext() const;
276 void unwindStateStack();
278 void applyStrokePattern();
279 void applyFillPattern();
281 void drawTextInternal(const String& text, float x, float y, bool fill, float maxWidth = 0, bool useMaxWidth = false);
283 const Font& accessFont();
285 #if ENABLE(DASHBOARD_SUPPORT)
286 void clearPathForDashboardBackwardCompatibilityMode();
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);
295 void prepareGradientForDashboard(CanvasGradient* gradient) const;
297 Vector<State, 1> m_stateStack;
298 bool m_usesCSSCompatibilityParseMode;
299 #if ENABLE(DASHBOARD_SUPPORT)
300 bool m_usesDashboardCompatibilityMode;
304 } // namespace WebCore