1 /* 2 * Copyright (C) 2006, 2007, 2009, 2010, 2011, 2012 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 "core/html/canvas/Canvas2DContextAttributes.h" 30 #include "core/html/canvas/CanvasPathMethods.h" 31 #include "core/html/canvas/CanvasRenderingContext.h" 32 #include "core/svg/SVGMatrix.h" 33 #include "platform/fonts/Font.h" 34 #include "platform/graphics/Color.h" 35 #include "platform/geometry/FloatSize.h" 36 #include "platform/graphics/GraphicsTypes.h" 37 #include "platform/graphics/ImageBuffer.h" 38 #include "platform/graphics/Path.h" 39 #include "platform/transforms/AffineTransform.h" 40 #include "wtf/HashMap.h" 41 #include "wtf/Vector.h" 42 #include "wtf/text/WTFString.h" 43 44 namespace blink { class WebLayer; } 45 46 namespace WebCore { 47 48 class CanvasGradient; 49 class CanvasPattern; 50 class CanvasStyle; 51 class DOMPath; 52 class Element; 53 class ExceptionState; 54 class FloatRect; 55 class GraphicsContext; 56 class HTMLCanvasElement; 57 class HTMLImageElement; 58 class HTMLVideoElement; 59 class ImageBitmap; 60 class ImageData; 61 class TextMetrics; 62 63 typedef HashMap<String, RefPtr<MutableStylePropertySet> > MutableStylePropertyMap; 64 65 class CanvasRenderingContext2D : public CanvasRenderingContext, public CanvasPathMethods { 66 public: 67 static PassOwnPtr<CanvasRenderingContext2D> create(HTMLCanvasElement* canvas, const Canvas2DContextAttributes* attrs, bool usesCSSCompatibilityParseMode) 68 { 69 return adoptPtr(new CanvasRenderingContext2D(canvas, attrs, usesCSSCompatibilityParseMode)); 70 } 71 virtual ~CanvasRenderingContext2D(); 72 73 CanvasStyle* strokeStyle() const; 74 void setStrokeStyle(PassRefPtr<CanvasStyle>); 75 76 CanvasStyle* fillStyle() const; 77 void setFillStyle(PassRefPtr<CanvasStyle>); 78 79 float lineWidth() const; 80 void setLineWidth(float); 81 82 String lineCap() const; 83 void setLineCap(const String&); 84 85 String lineJoin() const; 86 void setLineJoin(const String&); 87 88 float miterLimit() const; 89 void setMiterLimit(float); 90 91 const Vector<float>& getLineDash() const; 92 void setLineDash(const Vector<float>&); 93 void setWebkitLineDash(const Vector<float>&); 94 95 float lineDashOffset() const; 96 void setLineDashOffset(float); 97 float webkitLineDashOffset() const; 98 void setWebkitLineDashOffset(float); 99 100 float shadowOffsetX() const; 101 void setShadowOffsetX(float); 102 103 float shadowOffsetY() const; 104 void setShadowOffsetY(float); 105 106 float shadowBlur() const; 107 void setShadowBlur(float); 108 109 String shadowColor() const; 110 void setShadowColor(const String&); 111 112 float globalAlpha() const; 113 void setGlobalAlpha(float); 114 115 String globalCompositeOperation() const; 116 void setGlobalCompositeOperation(const String&); 117 118 void save() { ++m_unrealizedSaveCount; } 119 void restore(); 120 121 SVGMatrix currentTransform() const 122 { 123 return SVGMatrix(state().m_transform); 124 } 125 void setCurrentTransform(const SVGMatrix&); 126 127 void scale(float sx, float sy); 128 void rotate(float angleInRadians); 129 void translate(float tx, float ty); 130 void transform(float m11, float m12, float m21, float m22, float dx, float dy); 131 void setTransform(float m11, float m12, float m21, float m22, float dx, float dy); 132 void resetTransform(); 133 134 void setStrokeColor(const String& color); 135 void setStrokeColor(float grayLevel); 136 void setStrokeColor(const String& color, float alpha); 137 void setStrokeColor(float grayLevel, float alpha); 138 void setStrokeColor(float r, float g, float b, float a); 139 void setStrokeColor(float c, float m, float y, float k, float a); 140 141 void setFillColor(const String& color); 142 void setFillColor(float grayLevel); 143 void setFillColor(const String& color, float alpha); 144 void setFillColor(float grayLevel, float alpha); 145 void setFillColor(float r, float g, float b, float a); 146 void setFillColor(float c, float m, float y, float k, float a); 147 148 void beginPath(); 149 150 PassRefPtr<DOMPath> currentPath(); 151 void setCurrentPath(DOMPath*); 152 void fill(const String& winding = "nonzero"); 153 void stroke(); 154 void clip(const String& winding = "nonzero"); 155 156 bool isPointInPath(const float x, const float y, const String& winding = "nonzero"); 157 bool isPointInStroke(const float x, const float y); 158 159 void clearRect(float x, float y, float width, float height); 160 void fillRect(float x, float y, float width, float height); 161 void strokeRect(float x, float y, float width, float height); 162 163 void setShadow(float width, float height, float blur); 164 void setShadow(float width, float height, float blur, const String& color); 165 void setShadow(float width, float height, float blur, float grayLevel); 166 void setShadow(float width, float height, float blur, const String& color, float alpha); 167 void setShadow(float width, float height, float blur, float grayLevel, float alpha); 168 void setShadow(float width, float height, float blur, float r, float g, float b, float a); 169 void setShadow(float width, float height, float blur, float c, float m, float y, float k, float a); 170 171 void clearShadow(); 172 173 void drawImage(ImageBitmap*, float x, float y, ExceptionState&); 174 void drawImage(ImageBitmap*, float x, float y, float width, float height, ExceptionState&); 175 void drawImage(ImageBitmap*, float sx, float sy, float sw, float sh, float dx, float dy, float dw, float dh, ExceptionState&); 176 void drawImage(HTMLImageElement*, float x, float y, ExceptionState&); 177 void drawImage(HTMLImageElement*, float x, float y, float width, float height, ExceptionState&); 178 void drawImage(HTMLImageElement*, float sx, float sy, float sw, float sh, float dx, float dy, float dw, float dh, ExceptionState&); 179 void drawImage(HTMLImageElement*, const FloatRect& srcRect, const FloatRect& dstRect, ExceptionState&); 180 void drawImage(HTMLImageElement*, const FloatRect& srcRect, const FloatRect& dstRect, const CompositeOperator&, const blink::WebBlendMode&, ExceptionState&); 181 void drawImage(HTMLCanvasElement*, float x, float y, ExceptionState&); 182 void drawImage(HTMLCanvasElement*, float x, float y, float width, float height, ExceptionState&); 183 void drawImage(HTMLCanvasElement*, float sx, float sy, float sw, float sh, float dx, float dy, float dw, float dh, ExceptionState&); 184 void drawImage(HTMLCanvasElement*, const FloatRect& srcRect, const FloatRect& dstRect, ExceptionState&); 185 void drawImage(HTMLVideoElement*, float x, float y, ExceptionState&); 186 void drawImage(HTMLVideoElement*, float x, float y, float width, float height, ExceptionState&); 187 void drawImage(HTMLVideoElement*, float sx, float sy, float sw, float sh, float dx, float dy, float dw, float dh, ExceptionState&); 188 void drawImage(HTMLVideoElement*, const FloatRect& srcRect, const FloatRect& dstRect, ExceptionState&); 189 190 void drawImageFromRect(HTMLImageElement*, float sx = 0, float sy = 0, float sw = 0, float sh = 0, 191 float dx = 0, float dy = 0, float dw = 0, float dh = 0, const String& compositeOperation = emptyString()); 192 193 void setAlpha(float); 194 195 void setCompositeOperation(const String&); 196 197 PassRefPtr<CanvasGradient> createLinearGradient(float x0, float y0, float x1, float y1, ExceptionState&); 198 PassRefPtr<CanvasGradient> createRadialGradient(float x0, float y0, float r0, float x1, float y1, float r1, ExceptionState&); 199 PassRefPtr<CanvasPattern> createPattern(HTMLImageElement*, const String& repetitionType, ExceptionState&); 200 PassRefPtr<CanvasPattern> createPattern(HTMLCanvasElement*, const String& repetitionType, ExceptionState&); 201 202 PassRefPtr<ImageData> createImageData(PassRefPtr<ImageData>, ExceptionState&) const; 203 PassRefPtr<ImageData> createImageData(float width, float height, ExceptionState&) const; 204 PassRefPtr<ImageData> getImageData(float sx, float sy, float sw, float sh, ExceptionState&) const; 205 void putImageData(ImageData*, float dx, float dy, ExceptionState&); 206 void putImageData(ImageData*, float dx, float dy, float dirtyX, float dirtyY, float dirtyWidth, float dirtyHeight, ExceptionState&); 207 208 // Slated for deprecation: 209 void webkitPutImageDataHD(ImageData* image, float dx, float dy, ExceptionState& e) { putImageData(image, dx, dy, e); } 210 void webkitPutImageDataHD(ImageData* image, float dx, float dy, float dirtyX, float dirtyY, float dirtyWidth, float dirtyHeight, ExceptionState& e) { putImageData(image, dx, dy, dirtyX, dirtyY, dirtyWidth, dirtyHeight, e); } 211 PassRefPtr<ImageData> webkitGetImageDataHD(float sx, float sy, float sw, float sh, ExceptionState&) const; 212 float webkitBackingStorePixelRatio() const { return 1; } 213 214 void reset(); 215 216 String font() const; 217 void setFont(const String&); 218 219 String textAlign() const; 220 void setTextAlign(const String&); 221 222 String textBaseline() const; 223 void setTextBaseline(const String&); 224 225 void fillText(const String& text, float x, float y); 226 void fillText(const String& text, float x, float y, float maxWidth); 227 void strokeText(const String& text, float x, float y); 228 void strokeText(const String& text, float x, float y, float maxWidth); 229 PassRefPtr<TextMetrics> measureText(const String& text); 230 231 LineCap getLineCap() const { return state().m_lineCap; } 232 LineJoin getLineJoin() const { return state().m_lineJoin; } 233 234 bool imageSmoothingEnabled() const; 235 void setImageSmoothingEnabled(bool); 236 237 PassRefPtr<Canvas2DContextAttributes> getContextAttributes() const; 238 239 void drawSystemFocusRing(Element*); 240 bool drawCustomFocusRing(Element*); 241 242 private: 243 struct State : FontSelectorClient { 244 State(); 245 virtual ~State(); 246 247 State(const State&); 248 State& operator=(const State&); 249 250 virtual void fontsNeedUpdate(FontSelector*) OVERRIDE; 251 252 String m_unparsedStrokeColor; 253 String m_unparsedFillColor; 254 RefPtr<CanvasStyle> m_strokeStyle; 255 RefPtr<CanvasStyle> m_fillStyle; 256 float m_lineWidth; 257 LineCap m_lineCap; 258 LineJoin m_lineJoin; 259 float m_miterLimit; 260 FloatSize m_shadowOffset; 261 float m_shadowBlur; 262 RGBA32 m_shadowColor; 263 float m_globalAlpha; 264 CompositeOperator m_globalComposite; 265 blink::WebBlendMode m_globalBlend; 266 AffineTransform m_transform; 267 bool m_invertibleCTM; 268 Vector<float> m_lineDash; 269 float m_lineDashOffset; 270 bool m_imageSmoothingEnabled; 271 272 // Text state. 273 TextAlign m_textAlign; 274 TextBaseline m_textBaseline; 275 276 String m_unparsedFont; 277 Font m_font; 278 bool m_realizedFont; 279 }; 280 281 CanvasRenderingContext2D(HTMLCanvasElement*, const Canvas2DContextAttributes* attrs, bool usesCSSCompatibilityParseMode); 282 283 State& modifiableState() { ASSERT(!m_unrealizedSaveCount); return m_stateStack.last(); } 284 const State& state() const { return m_stateStack.last(); } 285 286 void applyLineDash() const; 287 void setShadow(const FloatSize& offset, float blur, RGBA32 color); 288 void applyShadow(); 289 bool shouldDrawShadows() const; 290 291 void drawImageInternal(Image*, const FloatRect&, const FloatRect&, const CompositeOperator&, const blink::WebBlendMode&); 292 bool computeDirtyRect(const FloatRect& localBounds, FloatRect*); 293 bool computeDirtyRect(const FloatRect& localBounds, const FloatRect& transformedClipBounds, FloatRect*); 294 void didDraw(const FloatRect&); 295 296 GraphicsContext* drawingContext() const; 297 298 void unwindStateStack(); 299 void realizeSaves() 300 { 301 if (m_unrealizedSaveCount) 302 realizeSavesLoop(); 303 } 304 void realizeSavesLoop(); 305 306 void applyStrokePattern(); 307 void applyFillPattern(); 308 309 void drawTextInternal(const String& text, float x, float y, bool fill, float maxWidth = 0, bool useMaxWidth = false); 310 311 const Font& accessFont(); 312 313 void clearCanvas(); 314 bool rectContainsTransformedRect(const FloatRect&, const FloatRect&) const; 315 316 void inflateStrokeRect(FloatRect&) const; 317 318 template<class T> void fullCanvasCompositedFill(const T&); 319 template<class T> void fullCanvasCompositedDrawImage(T*, const FloatRect&, const FloatRect&, CompositeOperator); 320 321 bool focusRingCallIsValid(const Path&, Element*); 322 void updateFocusRingAccessibility(const Path&, Element*); 323 void drawFocusRing(const Path&); 324 325 virtual bool is2d() const OVERRIDE { return true; } 326 virtual bool isAccelerated() const OVERRIDE; 327 virtual bool hasAlpha() const OVERRIDE { return m_hasAlpha; } 328 329 virtual bool isTransformInvertible() const { return state().m_invertibleCTM; } 330 331 virtual blink::WebLayer* platformLayer() const OVERRIDE; 332 333 Vector<State, 1> m_stateStack; 334 unsigned m_unrealizedSaveCount; 335 bool m_usesCSSCompatibilityParseMode; 336 bool m_hasAlpha; 337 MutableStylePropertyMap m_fetchedFonts; 338 }; 339 340 DEFINE_TYPE_CASTS(CanvasRenderingContext2D, CanvasRenderingContext, context, context->is2d(), context.is2d()); 341 342 } // namespace WebCore 343 344 #endif 345