2 * Copyright (C) 2006, 2007 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 "TransformationMatrix.h"
30 #include "FloatSize.h"
32 #include "GraphicsTypes.h"
34 #include "PlatformString.h"
35 #include <wtf/Vector.h>
38 #include <CoreGraphics/CoreGraphics.h>
47 class GraphicsContext
;
48 class HTMLCanvasElement
;
49 class HTMLImageElement
;
54 typedef int ExceptionCode
;
56 class CanvasRenderingContext2D
: Noncopyable
{
58 CanvasRenderingContext2D(HTMLCanvasElement
*);
63 HTMLCanvasElement
* canvas() const { return m_canvas
; }
65 CanvasStyle
* strokeStyle() const;
66 void setStrokeStyle(PassRefPtr
<CanvasStyle
>);
68 CanvasStyle
* fillStyle() const;
69 void setFillStyle(PassRefPtr
<CanvasStyle
>);
71 float lineWidth() const;
72 void setLineWidth(float);
74 String
lineCap() const;
75 void setLineCap(const String
&);
77 String
lineJoin() const;
78 void setLineJoin(const String
&);
80 float miterLimit() const;
81 void setMiterLimit(float);
83 float shadowOffsetX() const;
84 void setShadowOffsetX(float);
86 float shadowOffsetY() const;
87 void setShadowOffsetY(float);
89 float shadowBlur() const;
90 void setShadowBlur(float);
92 String
shadowColor() const;
93 void setShadowColor(const String
&);
95 float globalAlpha() const;
96 void setGlobalAlpha(float);
98 String
globalCompositeOperation() const;
99 void setGlobalCompositeOperation(const String
&);
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
);
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
);
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
);
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
);
139 bool isPointInPath(const float x
, const float y
);
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
);
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
);
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
&);
163 void drawImageFromRect(HTMLImageElement
*, float sx
, float sy
, float sw
, float sh
,
164 float dx
, float dy
, float dw
, float dh
, const String
& compositeOperation
);
166 void setAlpha(float);
168 void setCompositeOperation(const String
&);
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
&);
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
&);
183 void setFont(const String
&);
185 String
textAlign() const;
186 void setTextAlign(const String
&);
188 String
textBaseline() const;
189 void setTextBaseline(const String
&);
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
);
197 LineCap
getLineCap() const { return state().m_lineCap
; }
198 LineJoin
getLineJoin() const { return state().m_lineJoin
; }
204 RefPtr
<CanvasStyle
> m_strokeStyle
;
205 RefPtr
<CanvasStyle
> m_fillStyle
;
210 FloatSize m_shadowOffset
;
212 String m_shadowColor
;
214 CompositeOperator m_globalComposite
;
215 TransformationMatrix m_transform
;
216 bool m_invertibleCTM
;
219 TextAlign m_textAlign
;
220 TextBaseline m_textBaseline
;
222 String m_unparsedFont
;
228 State
& state() { return m_stateStack
.last(); }
229 const State
& state() const { return m_stateStack
.last(); }
233 enum CanvasWillDrawOption
{
234 CanvasWillDrawApplyTransform
= 1,
235 CanvasWillDrawApplyShadow
= 1 << 1,
236 CanvasWillDrawApplyClip
= 1 << 2,
237 CanvasWillDrawApplyAll
= 0xffffffff
240 void willDraw(const FloatRect
&, unsigned options
= CanvasWillDrawApplyAll
);
242 GraphicsContext
* drawingContext() const;
244 void applyStrokePattern();
245 void applyFillPattern();
247 void drawTextInternal(const String
& text
, float x
, float y
, bool fill
, float maxWidth
= 0, bool useMaxWidth
= false);
249 const Font
& accessFont();
251 #if ENABLE(DASHBOARD_SUPPORT)
252 void clearPathForDashboardBackwardCompatibilityMode();
255 void checkOrigin(const KURL
&);
257 HTMLCanvasElement
* m_canvas
;
258 Vector
<State
, 1> m_stateStack
;
261 } // namespace WebCore