2 * Copyright (C) 2004, 2006, 2007 Apple Inc. All rights reserved.
3 * Copyright (C) 2007 Alp Toker <alp@atoker.com>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
28 #include "HTMLCanvasElement.h"
30 #include "CanvasGradient.h"
31 #include "CanvasPattern.h"
32 #include "CanvasRenderingContext2D.h"
33 #include "CanvasStyle.h"
36 #include "ExceptionCode.h"
38 #include "GraphicsContext.h"
39 #include "HTMLNames.h"
40 #include "ImageBuffer.h"
41 #include "MIMETypeRegistry.h"
43 #include "RenderHTMLCanvas.h"
45 #include <kjs/interpreter.h>
58 using namespace HTMLNames;
60 // These values come from the WhatWG spec.
61 static const int defaultWidth = 300;
62 static const int defaultHeight = 150;
64 // Firefox limits width/height to 32767 pixels, but slows down dramatically before it
65 // reaches that limit. We limit by area instead, giving us larger maximum dimensions,
66 // in exchange for a smaller maximum canvas size.
67 const float HTMLCanvasElement::MaxCanvasArea = 32768 * 8192; // Maximum canvas area in CSS pixels
69 HTMLCanvasElement::HTMLCanvasElement(Document* doc)
70 : HTMLElement(canvasTag, doc)
71 , m_size(defaultWidth, defaultHeight)
74 , m_ignoreReset(false)
75 , m_createdImageBuffer(false)
79 HTMLCanvasElement::~HTMLCanvasElement()
83 #if ENABLE(DASHBOARD_SUPPORT)
85 HTMLTagStatus HTMLCanvasElement::endTagRequirement() const
87 Settings* settings = document()->settings();
88 if (settings && settings->usesDashboardBackwardCompatibilityMode())
89 return TagStatusForbidden;
91 return HTMLElement::endTagRequirement();
94 int HTMLCanvasElement::tagPriority() const
96 Settings* settings = document()->settings();
97 if (settings && settings->usesDashboardBackwardCompatibilityMode())
100 return HTMLElement::tagPriority();
105 void HTMLCanvasElement::parseMappedAttribute(MappedAttribute* attr)
107 const QualifiedName& attrName = attr->name();
108 if (attrName == widthAttr || attrName == heightAttr)
110 HTMLElement::parseMappedAttribute(attr);
113 RenderObject* HTMLCanvasElement::createRenderer(RenderArena* arena, RenderStyle* style)
115 Settings* settings = document()->settings();
116 if (settings && settings->isJavaScriptEnabled()) {
117 m_rendererIsCanvas = true;
118 return new (arena) RenderHTMLCanvas(this);
121 m_rendererIsCanvas = false;
122 return HTMLElement::createRenderer(arena, style);
125 void HTMLCanvasElement::setHeight(int value)
127 setAttribute(heightAttr, String::number(value));
130 void HTMLCanvasElement::setWidth(int value)
132 setAttribute(widthAttr, String::number(value));
135 String HTMLCanvasElement::toDataURL(const String& mimeType, ExceptionCode& ec)
137 if (!m_originClean) {
142 if (m_size.isEmpty())
143 return String("data:,");
145 if (mimeType.isNull() || !MIMETypeRegistry::isSupportedImageMIMETypeForEncoding(mimeType))
146 return buffer()->toDataURL("image/png");
148 return buffer()->toDataURL(mimeType);
151 CanvasRenderingContext* HTMLCanvasElement::getContext(const String& type)
155 m_2DContext.set(new CanvasRenderingContext2D(this));
156 return m_2DContext.get();
161 void HTMLCanvasElement::willDraw(const FloatRect& rect)
163 m_imageBuffer->clearImage();
165 if (RenderObject* ro = renderer()) {
166 #ifdef CANVAS_INCREMENTAL_REPAINT
167 // Handle CSS triggered scaling
168 float widthScale = static_cast<float>(ro->width()) / static_cast<float>(m_size.width());
169 float heightScale = static_cast<float>(ro->height()) / static_cast<float>(m_size.height());
170 FloatRect r(rect.x() * widthScale, rect.y() * heightScale, rect.width() * widthScale, rect.height() * heightScale);
171 ro->repaintRectangle(enclosingIntRect(r));
178 m_observer->canvasChanged(this, rect);
181 void HTMLCanvasElement::reset()
187 int w = getAttribute(widthAttr).toInt(&ok);
190 int h = getAttribute(heightAttr).toInt(&ok);
194 IntSize oldSize = m_size;
195 m_size = IntSize(w, h);
197 bool hadImageBuffer = m_createdImageBuffer;
198 m_createdImageBuffer = false;
199 m_imageBuffer.clear();
201 m_2DContext->reset();
203 if (RenderObject* ro = renderer())
204 if (m_rendererIsCanvas) {
205 if (oldSize != m_size)
206 static_cast<RenderHTMLCanvas*>(ro)->canvasSizeChanged();
212 m_observer->canvasResized(this);
215 void HTMLCanvasElement::paint(GraphicsContext* context, const IntRect& r)
217 if (context->paintingDisabled())
221 Image* image = m_imageBuffer->image();
223 context->drawImage(image, r);
227 IntRect HTMLCanvasElement::convertLogicalToDevice(const FloatRect& logicalRect) const
229 return IntRect(convertLogicalToDevice(logicalRect.location()), convertLogicalToDevice(logicalRect.size()));
232 IntSize HTMLCanvasElement::convertLogicalToDevice(const FloatSize& logicalSize) const
234 float pageScaleFactor = document()->frame() ? document()->frame()->page()->chrome()->scaleFactor() : 1.0f;
235 float wf = ceilf(logicalSize.width() * pageScaleFactor);
236 float hf = ceilf(logicalSize.height() * pageScaleFactor);
238 if (!(wf >= 1 && hf >= 1 && wf * hf <= MaxCanvasArea))
241 return IntSize(static_cast<unsigned>(wf), static_cast<unsigned>(hf));
244 IntPoint HTMLCanvasElement::convertLogicalToDevice(const FloatPoint& logicalPos) const
246 float pageScaleFactor = document()->frame() ? document()->frame()->page()->chrome()->scaleFactor() : 1.0f;
247 float xf = logicalPos.x() * pageScaleFactor;
248 float yf = logicalPos.y() * pageScaleFactor;
250 return IntPoint(static_cast<unsigned>(xf), static_cast<unsigned>(yf));
253 void HTMLCanvasElement::createImageBuffer() const
255 ASSERT(!m_imageBuffer);
257 m_createdImageBuffer = true;
259 FloatSize unscaledSize(width(), height());
260 IntSize size = convertLogicalToDevice(unscaledSize);
261 if (!size.width() || !size.height())
263 m_imageBuffer.set(ImageBuffer::create(size, false).release());
266 GraphicsContext* HTMLCanvasElement::drawingContext() const
268 return buffer() ? m_imageBuffer->context() : 0;
271 ImageBuffer* HTMLCanvasElement::buffer() const
273 if (!m_createdImageBuffer)
275 return m_imageBuffer.get();
280 CGImageRef HTMLCanvasElement::createPlatformImage() const
282 GraphicsContext* context = drawingContext();
286 CGContextRef contextRef = context->platformContext();
290 CGContextFlush(contextRef);
292 return CGBitmapContextCreateImage(contextRef);
297 QPixmap HTMLCanvasElement::createPlatformImage() const
301 return *m_imageBuffer->pixmap();
304 #elif PLATFORM(CAIRO)
306 cairo_surface_t* HTMLCanvasElement::createPlatformImage() const
311 // Note that unlike CG, our returned image is not a copy or
312 // copy-on-write, but the original. This is fine, since it is only
313 // ever used as a source.
315 cairo_surface_flush(m_imageBuffer->surface());
316 cairo_surface_reference(m_imageBuffer->surface());
317 return m_imageBuffer->surface();