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