2 * Copyright (C) 2006, 2007, 2009, 2010, 2011, 2012 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 "bindings/v8/ScriptWrappable.h"
30 #include "core/css/CSSFontSelectorClient.h"
31 #include "core/html/canvas/Canvas2DContextAttributes.h"
32 #include "core/html/canvas/CanvasPathMethods.h"
33 #include "core/html/canvas/CanvasRenderingContext.h"
34 #include "core/svg/SVGMatrixTearOff.h"
35 #include "platform/fonts/Font.h"
36 #include "platform/graphics/Color.h"
37 #include "platform/geometry/FloatSize.h"
38 #include "platform/graphics/GraphicsTypes.h"
39 #include "platform/graphics/ImageBuffer.h"
40 #include "platform/graphics/Path.h"
41 #include "platform/transforms/AffineTransform.h"
42 #include "wtf/HashMap.h"
43 #include "wtf/Vector.h"
44 #include "wtf/text/WTFString.h"
46 namespace blink { class WebLayer; }
50 class CanvasImageSource;
58 class GraphicsContext;
59 class HTMLCanvasElement;
60 class HTMLImageElement;
61 class HTMLVideoElement;
66 typedef WillBeHeapHashMap<String, RefPtrWillBeMember<MutableStylePropertySet> > MutableStylePropertyMap;
68 class CanvasRenderingContext2D FINAL: public CanvasRenderingContext, public ScriptWrappable, public CanvasPathMethods {
70 static PassOwnPtrWillBeRawPtr<CanvasRenderingContext2D> create(HTMLCanvasElement* canvas, const Canvas2DContextAttributes* attrs, bool usesCSSCompatibilityParseMode)
72 return adoptPtrWillBeNoop(new CanvasRenderingContext2D(canvas, attrs, usesCSSCompatibilityParseMode));
74 virtual ~CanvasRenderingContext2D();
76 CanvasStyle* strokeStyle() const;
77 void setStrokeStyle(PassRefPtr<CanvasStyle>);
79 CanvasStyle* fillStyle() const;
80 void setFillStyle(PassRefPtr<CanvasStyle>);
82 float lineWidth() const;
83 void setLineWidth(float);
85 String lineCap() const;
86 void setLineCap(const String&);
88 String lineJoin() const;
89 void setLineJoin(const String&);
91 float miterLimit() const;
92 void setMiterLimit(float);
94 const Vector<float>& getLineDash() const;
95 void setLineDash(const Vector<float>&);
97 float lineDashOffset() const;
98 void setLineDashOffset(float);
100 float shadowOffsetX() const;
101 void setShadowOffsetX(float);
103 float shadowOffsetY() const;
104 void setShadowOffsetY(float);
106 float shadowBlur() const;
107 void setShadowBlur(float);
109 String shadowColor() const;
110 void setShadowColor(const String&);
112 float globalAlpha() const;
113 void setGlobalAlpha(float);
115 bool isContextLost() const;
117 String globalCompositeOperation() const;
118 void setGlobalCompositeOperation(const String&);
120 void save() { ++m_stateStack.last()->m_unrealizedSaveCount; }
123 PassRefPtr<SVGMatrixTearOff> currentTransform() const
125 return SVGMatrixTearOff::create(state().m_transform);
127 void setCurrentTransform(PassRefPtr<SVGMatrixTearOff>);
129 void scale(float sx, float sy);
130 void rotate(float angleInRadians);
131 void translate(float tx, float ty);
132 void transform(float m11, float m12, float m21, float m22, float dx, float dy);
133 void setTransform(float m11, float m12, float m21, float m22, float dx, float dy);
134 void resetTransform();
136 void setStrokeColor(const String& color);
137 void setStrokeColor(float grayLevel);
138 void setStrokeColor(const String& color, float alpha);
139 void setStrokeColor(float grayLevel, float alpha);
140 void setStrokeColor(float r, float g, float b, float a);
141 void setStrokeColor(float c, float m, float y, float k, float a);
143 void setFillColor(const String& color);
144 void setFillColor(float grayLevel);
145 void setFillColor(const String& color, float alpha);
146 void setFillColor(float grayLevel, float alpha);
147 void setFillColor(float r, float g, float b, float a);
148 void setFillColor(float c, float m, float y, float k, float a);
152 void fill(const String& winding = "nonzero");
153 void fill(Path2D*, const String& winding = "nonzero");
155 void stroke(Path2D*);
156 void clip(const String& winding = "nonzero");
157 void clip(Path2D*, const String& winding = "nonzero");
159 bool isPointInPath(const float x, const float y, const String& winding = "nonzero");
160 bool isPointInPath(Path2D*, const float x, const float y, const String& winding = "nonzero");
161 bool isPointInStroke(const float x, const float y);
162 bool isPointInStroke(Path2D*, const float x, const float y);
164 void scrollPathIntoView();
165 void scrollPathIntoView(Path2D*);
167 void clearRect(float x, float y, float width, float height);
168 void fillRect(float x, float y, float width, float height);
169 void strokeRect(float x, float y, float width, float height);
171 void setShadow(float width, float height, float blur);
172 void setShadow(float width, float height, float blur, const String& color);
173 void setShadow(float width, float height, float blur, float grayLevel);
174 void setShadow(float width, float height, float blur, const String& color, float alpha);
175 void setShadow(float width, float height, float blur, float grayLevel, float alpha);
176 void setShadow(float width, float height, float blur, float r, float g, float b, float a);
177 void setShadow(float width, float height, float blur, float c, float m, float y, float k, float a);
181 void drawImage(CanvasImageSource*, float x, float y, ExceptionState&);
182 void drawImage(CanvasImageSource*, float x, float y, float width, float height, ExceptionState&);
183 void drawImage(CanvasImageSource*, float sx, float sy, float sw, float sh, float dx, float dy, float dw, float dh, ExceptionState&);
185 void drawImageFromRect(HTMLImageElement*, float sx = 0, float sy = 0, float sw = 0, float sh = 0,
186 float dx = 0, float dy = 0, float dw = 0, float dh = 0, const String& compositeOperation = emptyString());
188 void setAlpha(float);
190 void setCompositeOperation(const String&);
192 PassRefPtr<CanvasGradient> createLinearGradient(float x0, float y0, float x1, float y1);
193 PassRefPtr<CanvasGradient> createRadialGradient(float x0, float y0, float r0, float x1, float y1, float r1, ExceptionState&);
194 PassRefPtr<CanvasPattern> createPattern(CanvasImageSource*, const String& repetitionType, ExceptionState&);
196 PassRefPtrWillBeRawPtr<ImageData> createImageData(PassRefPtrWillBeRawPtr<ImageData>) const;
197 PassRefPtrWillBeRawPtr<ImageData> createImageData(float width, float height, ExceptionState&) const;
198 PassRefPtrWillBeRawPtr<ImageData> getImageData(float sx, float sy, float sw, float sh, ExceptionState&) const;
199 void putImageData(ImageData*, float dx, float dy);
200 void putImageData(ImageData*, float dx, float dy, float dirtyX, float dirtyY, float dirtyWidth, float dirtyHeight);
205 void setFont(const String&);
207 String textAlign() const;
208 void setTextAlign(const String&);
210 String textBaseline() const;
211 void setTextBaseline(const String&);
213 void fillText(const String& text, float x, float y);
214 void fillText(const String& text, float x, float y, float maxWidth);
215 void strokeText(const String& text, float x, float y);
216 void strokeText(const String& text, float x, float y, float maxWidth);
217 PassRefPtr<TextMetrics> measureText(const String& text);
219 LineCap getLineCap() const { return state().m_lineCap; }
220 LineJoin getLineJoin() const { return state().m_lineJoin; }
222 bool imageSmoothingEnabled() const;
223 void setImageSmoothingEnabled(bool);
225 PassRefPtr<Canvas2DContextAttributes> getContextAttributes() const;
227 void drawFocusIfNeeded(Element*);
228 void drawFocusIfNeeded(Path2D*, Element*);
229 bool drawCustomFocusRing(Element*);
232 void restoreContext();
234 virtual void trace(Visitor*) OVERRIDE;
237 class State FINAL : public CSSFontSelectorClient {
243 State& operator=(const State&);
245 // CSSFontSelectorClient implementation
246 virtual void fontsNeedUpdate(CSSFontSelector*) OVERRIDE;
248 virtual void trace(Visitor* visitor) OVERRIDE { CSSFontSelectorClient::trace(visitor); }
250 unsigned m_unrealizedSaveCount;
252 String m_unparsedStrokeColor;
253 String m_unparsedFillColor;
254 RefPtr<CanvasStyle> m_strokeStyle;
255 RefPtr<CanvasStyle> m_fillStyle;
260 FloatSize m_shadowOffset;
262 RGBA32 m_shadowColor;
264 CompositeOperator m_globalComposite;
265 blink::WebBlendMode m_globalBlend;
266 AffineTransform m_transform;
267 bool m_invertibleCTM;
268 Vector<float> m_lineDash;
269 float m_lineDashOffset;
270 bool m_imageSmoothingEnabled;
273 TextAlign m_textAlign;
274 TextBaseline m_textBaseline;
276 String m_unparsedFont;
281 CanvasRenderingContext2D(HTMLCanvasElement*, const Canvas2DContextAttributes* attrs, bool usesCSSCompatibilityParseMode);
283 State& modifiableState() { ASSERT(!state().m_unrealizedSaveCount); return *m_stateStack.last(); }
284 const State& state() const { return *m_stateStack.last(); }
286 void applyLineDash() const;
287 void setShadow(const FloatSize& offset, float blur, RGBA32 color);
289 bool shouldDrawShadows() const;
291 void dispatchContextLostEvent(Timer<CanvasRenderingContext2D>*);
292 void dispatchContextRestoredEvent(Timer<CanvasRenderingContext2D>*);
293 void tryRestoreContextEvent(Timer<CanvasRenderingContext2D>*);
295 bool computeDirtyRect(const FloatRect& localBounds, FloatRect*);
296 bool computeDirtyRect(const FloatRect& localBounds, const FloatRect& transformedClipBounds, FloatRect*);
297 void didDraw(const FloatRect&);
299 GraphicsContext* drawingContext() const;
301 void unwindStateStack();
304 void applyStrokePattern();
305 void applyFillPattern();
307 void drawImageInternal(CanvasImageSource*, float sx, float sy, float sw, float sh, float dx, float dy, float dw, float dh, ExceptionState&, CompositeOperator, blink::WebBlendMode);
308 void drawVideo(HTMLVideoElement*, FloatRect srcRect, FloatRect dstRect);
310 void fillInternal(const Path&, const String& windingRuleString);
311 void strokeInternal(const Path&);
312 void clipInternal(const Path&, const String& windingRuleString);
314 bool isPointInPathInternal(const Path&, const float x, const float y, const String& windingRuleString);
315 bool isPointInStrokeInternal(const Path&, const float x, const float y);
317 void scrollPathIntoViewInternal(const Path&);
319 void drawTextInternal(const String& text, float x, float y, bool fill, float maxWidth = 0, bool useMaxWidth = false);
321 const Font& accessFont();
322 int getFontBaseline(const FontMetrics&) const;
325 bool rectContainsTransformedRect(const FloatRect&, const FloatRect&) const;
327 void inflateStrokeRect(FloatRect&) const;
329 template<class T> void fullCanvasCompositedFill(const T&);
330 template<class T> void fullCanvasCompositedStroke(const T&);
331 template<class T> void fullCanvasCompositedDrawImage(T*, const FloatRect&, const FloatRect&, CompositeOperator);
333 void drawFocusIfNeededInternal(const Path&, Element*);
334 bool focusRingCallIsValid(const Path&, Element*);
335 void updateFocusRingAccessibility(const Path&, Element*);
336 void drawFocusRing(const Path&);
338 virtual bool is2d() const OVERRIDE { return true; }
339 virtual bool isAccelerated() const OVERRIDE;
340 virtual bool hasAlpha() const OVERRIDE { return m_hasAlpha; }
342 virtual bool isTransformInvertible() const OVERRIDE { return state().m_invertibleCTM; }
344 virtual blink::WebLayer* platformLayer() const OVERRIDE;
346 WillBeHeapVector<OwnPtrWillBeMember<State> > m_stateStack;
347 bool m_usesCSSCompatibilityParseMode;
349 bool m_isContextLost;
350 bool m_contextRestorable;
351 Canvas2DContextStorage m_storageMode;
352 MutableStylePropertyMap m_fetchedFonts;
353 unsigned m_tryRestoreContextAttemptCount;
354 Timer<CanvasRenderingContext2D> m_dispatchContextLostEventTimer;
355 Timer<CanvasRenderingContext2D> m_dispatchContextRestoredEventTimer;
356 Timer<CanvasRenderingContext2D> m_tryRestoreContextEventTimer;
359 DEFINE_TYPE_CASTS(CanvasRenderingContext2D, CanvasRenderingContext, context, context->is2d(), context.is2d());
361 } // namespace WebCore