Home | History | Annotate | Download | only in html
      1 /*
      2  * Copyright (C) 2004, 2006, 2009 Apple Inc. All rights reserved.
      3  * Copyright (C) 2007 Alp Toker <alp (at) atoker.com>
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions
      7  * are met:
      8  * 1. Redistributions of source code must retain the above copyright
      9  *    notice, this list of conditions and the following disclaimer.
     10  * 2. Redistributions in binary form must reproduce the above copyright
     11  *    notice, this list of conditions and the following disclaimer in the
     12  *    documentation and/or other materials provided with the distribution.
     13  *
     14  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
     15  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     17  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
     18  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     19  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     20  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     21  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
     22  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     24  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     25  */
     26 
     27 #ifndef HTMLCanvasElement_h
     28 #define HTMLCanvasElement_h
     29 
     30 #include "AffineTransform.h"
     31 #include "FloatRect.h"
     32 #include "HTMLElement.h"
     33 #if ENABLE(3D_CANVAS)
     34 #include "GraphicsContext3D.h"
     35 #endif
     36 #include "IntSize.h"
     37 
     38 namespace WebCore {
     39 
     40 class CanvasContextAttributes;
     41 class CanvasRenderingContext;
     42 class FloatPoint;
     43 class FloatRect;
     44 class FloatSize;
     45 class GraphicsContext;
     46 class HTMLCanvasElement;
     47 class ImageBuffer;
     48 class IntPoint;
     49 class IntSize;
     50 
     51 class CanvasObserver {
     52 public:
     53     virtual ~CanvasObserver() { }
     54 
     55     virtual void canvasChanged(HTMLCanvasElement*, const FloatRect& changedRect) = 0;
     56     virtual void canvasResized(HTMLCanvasElement*) = 0;
     57     virtual void canvasDestroyed(HTMLCanvasElement*) = 0;
     58 };
     59 
     60 class HTMLCanvasElement : public HTMLElement {
     61 public:
     62     HTMLCanvasElement(const QualifiedName&, Document*);
     63     virtual ~HTMLCanvasElement();
     64 
     65     int width() const { return m_size.width(); }
     66     int height() const { return m_size.height(); }
     67     void setWidth(int);
     68     void setHeight(int);
     69 
     70     String toDataURL(const String& mimeType, ExceptionCode&);
     71 
     72     CanvasRenderingContext* getContext(const String&, CanvasContextAttributes* attributes = 0);
     73 
     74     const IntSize& size() const { return m_size; }
     75     void setSize(const IntSize& size)
     76     {
     77         if (size == m_size)
     78             return;
     79         m_ignoreReset = true;
     80         setWidth(size.width());
     81         setHeight(size.height());
     82         m_ignoreReset = false;
     83         reset();
     84     }
     85 
     86     void willDraw(const FloatRect&);
     87 
     88     void paint(GraphicsContext*, const IntRect&);
     89 
     90     GraphicsContext* drawingContext() const;
     91 
     92     ImageBuffer* buffer() const;
     93 
     94     IntRect convertLogicalToDevice(const FloatRect&) const;
     95     IntSize convertLogicalToDevice(const FloatSize&) const;
     96     IntPoint convertLogicalToDevice(const FloatPoint&) const;
     97 
     98     void setOriginTainted() { m_originClean = false; }
     99     bool originClean() const { return m_originClean; }
    100 
    101     void setObserver(CanvasObserver* observer) { m_observer = observer; }
    102 
    103     AffineTransform baseTransform() const;
    104 
    105     CanvasRenderingContext* renderingContext() const { return m_context.get(); }
    106 
    107 #if ENABLE(3D_CANVAS)
    108     bool is3D() const;
    109 #endif
    110 
    111 private:
    112 #if ENABLE(DASHBOARD_SUPPORT)
    113     virtual HTMLTagStatus endTagRequirement() const;
    114     virtual int tagPriority() const;
    115 #endif
    116 
    117     virtual void parseMappedAttribute(MappedAttribute*);
    118     virtual RenderObject* createRenderer(RenderArena*, RenderStyle*);
    119 
    120     void createImageBuffer() const;
    121     void reset();
    122 
    123     static const float MaxCanvasArea;
    124 
    125     bool m_rendererIsCanvas;
    126 
    127     OwnPtr<CanvasRenderingContext> m_context;
    128     IntSize m_size;
    129     CanvasObserver* m_observer;
    130 
    131     bool m_originClean;
    132     bool m_ignoreReset;
    133     FloatRect m_dirtyRect;
    134 
    135     // m_createdImageBuffer means we tried to malloc the buffer.  We didn't necessarily get it.
    136     mutable bool m_createdImageBuffer;
    137     mutable OwnPtr<ImageBuffer> m_imageBuffer;
    138 };
    139 
    140 } //namespace
    141 
    142 #endif
    143