• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "TransformationMatrix.h"
30 #include "FloatSize.h"
31 #include "Font.h"
32 #include "GraphicsTypes.h"
33 #include "Path.h"
34 #include "PlatformString.h"
35 #include <wtf/Vector.h>
36 
37 #if PLATFORM(CG)
38 #include <ApplicationServices/ApplicationServices.h>
39 #endif
40 
41 namespace WebCore {
42 
43     class CanvasGradient;
44     class CanvasPattern;
45     class CanvasStyle;
46     class FloatRect;
47     class GraphicsContext;
48     class HTMLCanvasElement;
49     class HTMLImageElement;
50     class HTMLVideoElement;
51     class ImageData;
52     class KURL;
53     class TextMetrics;
54 
55     typedef int ExceptionCode;
56 
57     class CanvasRenderingContext2D : public Noncopyable {
58     public:
59         CanvasRenderingContext2D(HTMLCanvasElement*);
60 
61         void ref();
62         void deref();
63 
canvas()64         HTMLCanvasElement* canvas() const { return m_canvas; }
65 
66         CanvasStyle* strokeStyle() const;
67         void setStrokeStyle(PassRefPtr<CanvasStyle>);
68 
69         CanvasStyle* fillStyle() const;
70         void setFillStyle(PassRefPtr<CanvasStyle>);
71 
72         float lineWidth() const;
73         void setLineWidth(float);
74 
75         String lineCap() const;
76         void setLineCap(const String&);
77 
78         String lineJoin() const;
79         void setLineJoin(const String&);
80 
81         float miterLimit() const;
82         void setMiterLimit(float);
83 
84         float shadowOffsetX() const;
85         void setShadowOffsetX(float);
86 
87         float shadowOffsetY() const;
88         void setShadowOffsetY(float);
89 
90         float shadowBlur() const;
91         void setShadowBlur(float);
92 
93         String shadowColor() const;
94         void setShadowColor(const String&);
95 
96         float globalAlpha() const;
97         void setGlobalAlpha(float);
98 
99         String globalCompositeOperation() const;
100         void setGlobalCompositeOperation(const String&);
101 
102         void save();
103         void restore();
104 
105         void scale(float sx, float sy);
106         void rotate(float angleInRadians);
107         void translate(float tx, float ty);
108         void transform(float m11, float m12, float m21, float m22, float dx, float dy);
109         void setTransform(float m11, float m12, float m21, float m22, float dx, float dy);
110 
111         void setStrokeColor(const String& color);
112         void setStrokeColor(float grayLevel);
113         void setStrokeColor(const String& color, float alpha);
114         void setStrokeColor(float grayLevel, float alpha);
115         void setStrokeColor(float r, float g, float b, float a);
116         void setStrokeColor(float c, float m, float y, float k, float a);
117 
118         void setFillColor(const String& color);
119         void setFillColor(float grayLevel);
120         void setFillColor(const String& color, float alpha);
121         void setFillColor(float grayLevel, float alpha);
122         void setFillColor(float r, float g, float b, float a);
123         void setFillColor(float c, float m, float y, float k, float a);
124 
125         void beginPath();
126         void closePath();
127 
128         void moveTo(float x, float y);
129         void lineTo(float x, float y);
130         void quadraticCurveTo(float cpx, float cpy, float x, float y);
131         void bezierCurveTo(float cp1x, float cp1y, float cp2x, float cp2y, float x, float y);
132         void arcTo(float x0, float y0, float x1, float y1, float radius, ExceptionCode&);
133         void arc(float x, float y, float r, float sa, float ea, bool clockwise, ExceptionCode&);
134         void rect(float x, float y, float width, float height);
135 
136         void fill();
137         void stroke();
138         void clip();
139 
140         bool isPointInPath(const float x, const float y);
141 
142         void clearRect(float x, float y, float width, float height);
143         void fillRect(float x, float y, float width, float height);
144         void strokeRect(float x, float y, float width, float height);
145         void strokeRect(float x, float y, float width, float height, float lineWidth);
146 
147         void setShadow(float width, float height, float blur);
148         void setShadow(float width, float height, float blur, const String& color);
149         void setShadow(float width, float height, float blur, float grayLevel);
150         void setShadow(float width, float height, float blur, const String& color, float alpha);
151         void setShadow(float width, float height, float blur, float grayLevel, float alpha);
152         void setShadow(float width, float height, float blur, float r, float g, float b, float a);
153         void setShadow(float width, float height, float blur, float c, float m, float y, float k, float a);
154 
155         void clearShadow();
156 
157         void drawImage(HTMLImageElement*, float x, float y);
158         void drawImage(HTMLImageElement*, float x, float y, float width, float height, ExceptionCode&);
159         void drawImage(HTMLImageElement*, const FloatRect& srcRect, const FloatRect& dstRect, ExceptionCode&);
160         void drawImage(HTMLCanvasElement*, float x, float y);
161         void drawImage(HTMLCanvasElement*, float x, float y, float width, float height, ExceptionCode&);
162         void drawImage(HTMLCanvasElement*, const FloatRect& srcRect, const FloatRect& dstRect, ExceptionCode&);
163 #if ENABLE(VIDEO)
164         void drawImage(HTMLVideoElement*, float x, float y);
165         void drawImage(HTMLVideoElement*, float x, float y, float width, float height, ExceptionCode&);
166         void drawImage(HTMLVideoElement*, const FloatRect& srcRect, const FloatRect& dstRect, ExceptionCode&);
167 #endif
168 
169         void drawImageFromRect(HTMLImageElement*, float sx, float sy, float sw, float sh,
170             float dx, float dy, float dw, float dh, const String& compositeOperation);
171 
172         void setAlpha(float);
173 
174         void setCompositeOperation(const String&);
175 
176         PassRefPtr<CanvasGradient> createLinearGradient(float x0, float y0, float x1, float y1, ExceptionCode&);
177         PassRefPtr<CanvasGradient> createRadialGradient(float x0, float y0, float r0, float x1, float y1, float r1, ExceptionCode&);
178         PassRefPtr<CanvasPattern> createPattern(HTMLImageElement*, const String& repetitionType, ExceptionCode&);
179         PassRefPtr<CanvasPattern> createPattern(HTMLCanvasElement*, const String& repetitionType, ExceptionCode&);
180 
181         PassRefPtr<ImageData> createImageData(float width, float height) const;
182         PassRefPtr<ImageData> getImageData(float sx, float sy, float sw, float sh, ExceptionCode&) const;
183         void putImageData(ImageData*, float dx, float dy, ExceptionCode&);
184         void putImageData(ImageData*, float dx, float dy, float dirtyX, float dirtyY, float dirtyWidth, float dirtyHeight, ExceptionCode&);
185 
186         void reset();
187 
188         String font() const;
189         void setFont(const String&);
190 
191         String textAlign() const;
192         void setTextAlign(const String&);
193 
194         String textBaseline() const;
195         void setTextBaseline(const String&);
196 
197         void fillText(const String& text, float x, float y);
198         void fillText(const String& text, float x, float y, float maxWidth);
199         void strokeText(const String& text, float x, float y);
200         void strokeText(const String& text, float x, float y, float maxWidth);
201         PassRefPtr<TextMetrics> measureText(const String& text);
202 
getLineCap()203         LineCap getLineCap() const { return state().m_lineCap; }
getLineJoin()204         LineJoin getLineJoin() const { return state().m_lineJoin; }
205 
206     private:
207         struct State {
208             State();
209 
210             RefPtr<CanvasStyle> m_strokeStyle;
211             RefPtr<CanvasStyle> m_fillStyle;
212             float m_lineWidth;
213             LineCap m_lineCap;
214             LineJoin m_lineJoin;
215             float m_miterLimit;
216             FloatSize m_shadowOffset;
217             float m_shadowBlur;
218             String m_shadowColor;
219             float m_globalAlpha;
220             CompositeOperator m_globalComposite;
221             TransformationMatrix m_transform;
222             bool m_invertibleCTM;
223 
224             // Text state.
225             TextAlign m_textAlign;
226             TextBaseline m_textBaseline;
227 
228             String m_unparsedFont;
229             Font m_font;
230             bool m_realizedFont;
231         };
232         Path m_path;
233 
state()234         State& state() { return m_stateStack.last(); }
state()235         const State& state() const { return m_stateStack.last(); }
236 
237         void applyShadow();
238 
239         enum CanvasWillDrawOption {
240             CanvasWillDrawApplyTransform = 1,
241             CanvasWillDrawApplyShadow = 1 << 1,
242             CanvasWillDrawApplyClip = 1 << 2,
243             CanvasWillDrawApplyAll = 0xffffffff
244         };
245 
246         void willDraw(const FloatRect&, unsigned options = CanvasWillDrawApplyAll);
247 
248         GraphicsContext* drawingContext() const;
249 
250         void applyStrokePattern();
251         void applyFillPattern();
252 
253         void drawTextInternal(const String& text, float x, float y, bool fill, float maxWidth = 0, bool useMaxWidth = false);
254 
255         const Font& accessFont();
256 
257 #if ENABLE(DASHBOARD_SUPPORT)
258         void clearPathForDashboardBackwardCompatibilityMode();
259 #endif
260 
261         void prepareGradientForDashboard(CanvasGradient* gradient) const;
262         void checkOrigin(const KURL&);
263         void checkOrigin(const String&);
264 
265         HTMLCanvasElement* m_canvas;
266         Vector<State, 1> m_stateStack;
267     };
268 
269 } // namespace WebCore
270 
271 #endif
272