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