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