2 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012 Apple Inc. All rights reserved.
3 * Copyright (C) 2008, 2010 Nokia Corporation and/or its subsidiary(-ies)
4 * Copyright (C) 2007 Alp Toker <alp@atoker.com>
5 * Copyright (C) 2008 Eric Seidel <eric@webkit.org>
6 * Copyright (C) 2008 Dirk Schulze <krit@webkit.org>
7 * Copyright (C) 2010 Torch Mobile (Beijing) Co. Ltd. All rights reserved.
8 * Copyright (C) 2012, 2013 Intel Corporation. All rights reserved.
9 * Copyright (C) 2013 Adobe Systems Incorporated. All rights reserved.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
21 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
28 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #include "core/html/canvas/CanvasRenderingContext2D.h"
36 #include "bindings/core/v8/ExceptionMessages.h"
37 #include "bindings/core/v8/ExceptionState.h"
38 #include "bindings/core/v8/ExceptionStatePlaceholder.h"
39 #include "core/CSSPropertyNames.h"
40 #include "core/css/CSSFontSelector.h"
41 #include "core/css/StylePropertySet.h"
42 #include "core/css/parser/CSSParser.h"
43 #include "core/css/resolver/StyleResolver.h"
44 #include "core/dom/ExceptionCode.h"
45 #include "core/dom/StyleEngine.h"
46 #include "core/events/Event.h"
47 #include "core/fetch/ImageResource.h"
48 #include "core/frame/ImageBitmap.h"
49 #include "core/frame/Settings.h"
50 #include "core/frame/UseCounter.h"
51 #include "core/html/HTMLCanvasElement.h"
52 #include "core/html/HTMLImageElement.h"
53 #include "core/html/HTMLMediaElement.h"
54 #include "core/html/HTMLVideoElement.h"
55 #include "core/html/ImageData.h"
56 #include "core/html/TextMetrics.h"
57 #include "core/html/canvas/CanvasGradient.h"
58 #include "core/html/canvas/CanvasPattern.h"
59 #include "core/html/canvas/CanvasStyle.h"
60 #include "core/html/canvas/Path2D.h"
61 #include "core/rendering/RenderImage.h"
62 #include "core/rendering/RenderLayer.h"
63 #include "core/rendering/RenderTheme.h"
64 #include "platform/fonts/FontCache.h"
65 #include "platform/geometry/FloatQuad.h"
66 #include "platform/graphics/DrawLooperBuilder.h"
67 #include "platform/graphics/GraphicsContextStateSaver.h"
68 #include "platform/text/TextRun.h"
69 #include "wtf/CheckedArithmetic.h"
70 #include "wtf/MathExtras.h"
71 #include "wtf/OwnPtr.h"
72 #include "wtf/Uint8ClampedArray.h"
73 #include "wtf/text/StringBuilder.h"
77 static const int defaultFontSize = 10;
78 static const char defaultFontFamily[] = "sans-serif";
79 static const char defaultFont[] = "10px sans-serif";
80 static const char inherit[] = "inherit";
81 static const char rtl[] = "rtl";
82 static const char ltr[] = "ltr";
83 static const double TryRestoreContextInterval = 0.5;
84 static const unsigned MaxTryRestoreContextAttempts = 4;
85 static const unsigned FetchedFontsCacheLimit = 50;
87 static bool contextLostRestoredEventsEnabled()
89 return RuntimeEnabledFeatures::experimentalCanvasFeaturesEnabled();
92 CanvasRenderingContext2D::CanvasRenderingContext2D(HTMLCanvasElement* canvas, const Canvas2DContextAttributes* attrs, Document& document)
93 : CanvasRenderingContext(canvas)
94 , m_usesCSSCompatibilityParseMode(document.inQuirksMode())
95 , m_clipAntialiasing(GraphicsContext::NotAntiAliased)
96 , m_hasAlpha(!attrs || attrs->alpha())
97 , m_isContextLost(false)
98 , m_contextRestorable(true)
99 , m_storageMode(!attrs ? PersistentStorage : attrs->parsedStorage())
100 , m_tryRestoreContextAttemptCount(0)
101 , m_dispatchContextLostEventTimer(this, &CanvasRenderingContext2D::dispatchContextLostEvent)
102 , m_dispatchContextRestoredEventTimer(this, &CanvasRenderingContext2D::dispatchContextRestoredEvent)
103 , m_tryRestoreContextEventTimer(this, &CanvasRenderingContext2D::tryRestoreContextEvent)
105 if (document.settings() && document.settings()->antialiasedClips2dCanvasEnabled())
106 m_clipAntialiasing = GraphicsContext::AntiAliased;
107 m_stateStack.append(adoptPtrWillBeNoop(new State()));
110 void CanvasRenderingContext2D::unwindStateStack()
112 if (size_t stackSize = m_stateStack.size()) {
113 if (GraphicsContext* context = canvas()->existingDrawingContext()) {
120 CanvasRenderingContext2D::~CanvasRenderingContext2D()
124 void CanvasRenderingContext2D::validateStateStack()
127 GraphicsContext* context = canvas()->existingDrawingContext();
128 if (context && !context->contextDisabled() && !m_isContextLost)
129 ASSERT(context->saveCount() == m_stateStack.size());
133 bool CanvasRenderingContext2D::isAccelerated() const
135 if (!canvas()->hasImageBuffer())
137 GraphicsContext* context = drawingContext();
138 return context && context->isAccelerated();
141 bool CanvasRenderingContext2D::isContextLost() const
143 return m_isContextLost;
146 void CanvasRenderingContext2D::loseContext()
150 m_isContextLost = true;
151 m_dispatchContextLostEventTimer.startOneShot(0, FROM_HERE);
154 void CanvasRenderingContext2D::restoreContext()
156 if (!m_contextRestorable)
158 // This code path is for restoring from an eviction
159 // Restoring from surface failure is handled internally
160 ASSERT(m_isContextLost && !canvas()->hasImageBuffer());
162 if (canvas()->buffer()) {
163 if (contextLostRestoredEventsEnabled()) {
164 m_dispatchContextRestoredEventTimer.startOneShot(0, FROM_HERE);
166 // legacy synchronous context restoration.
168 m_isContextLost = false;
173 void CanvasRenderingContext2D::trace(Visitor* visitor)
176 visitor->trace(m_stateStack);
177 visitor->trace(m_fetchedFonts);
178 visitor->trace(m_hitRegionManager);
180 CanvasRenderingContext::trace(visitor);
183 void CanvasRenderingContext2D::dispatchContextLostEvent(Timer<CanvasRenderingContext2D>*)
185 if (contextLostRestoredEventsEnabled()) {
186 RefPtrWillBeRawPtr<Event> event = Event::createCancelable(EventTypeNames::contextlost);
187 canvas()->dispatchEvent(event);
188 if (event->defaultPrevented()) {
189 m_contextRestorable = false;
193 // If an image buffer is present, it means the context was not lost due to
194 // an eviction, but rather due to a surface failure (gpu context lost?)
195 if (m_contextRestorable && canvas()->hasImageBuffer()) {
196 m_tryRestoreContextAttemptCount = 0;
197 m_tryRestoreContextEventTimer.startRepeating(TryRestoreContextInterval, FROM_HERE);
201 void CanvasRenderingContext2D::tryRestoreContextEvent(Timer<CanvasRenderingContext2D>* timer)
203 if (!m_isContextLost) {
204 // Canvas was already restored (possibly thanks to a resize), so stop trying.
205 m_tryRestoreContextEventTimer.stop();
208 if (canvas()->hasImageBuffer() && canvas()->buffer()->restoreSurface()) {
209 m_tryRestoreContextEventTimer.stop();
210 dispatchContextRestoredEvent(nullptr);
213 if (++m_tryRestoreContextAttemptCount > MaxTryRestoreContextAttempts)
214 canvas()->discardImageBuffer();
216 if (!canvas()->hasImageBuffer()) {
217 // final attempt: allocate a brand new image buffer instead of restoring
219 if (canvas()->buffer())
220 dispatchContextRestoredEvent(nullptr);
224 void CanvasRenderingContext2D::dispatchContextRestoredEvent(Timer<CanvasRenderingContext2D>*)
226 if (!m_isContextLost)
229 m_isContextLost = false;
230 if (contextLostRestoredEventsEnabled()) {
231 RefPtrWillBeRawPtr<Event> event(Event::create(EventTypeNames::contextrestored));
232 canvas()->dispatchEvent(event);
236 void CanvasRenderingContext2D::reset()
238 validateStateStack();
240 m_stateStack.resize(1);
241 m_stateStack.first() = adoptPtrWillBeNoop(new State());
243 validateStateStack();
246 // Important: Several of these properties are also stored in GraphicsContext's
247 // StrokeData. The default values that StrokeData uses may not the same values
248 // that the canvas 2d spec specifies. Make sure to sync the initial state of the
249 // GraphicsContext in HTMLCanvasElement::createImageBuffer()!
250 CanvasRenderingContext2D::State::State()
251 : m_unrealizedSaveCount(0)
252 , m_strokeStyle(CanvasStyle::createFromRGBA(Color::black))
253 , m_fillStyle(CanvasStyle::createFromRGBA(Color::black))
256 , m_lineJoin(MiterJoin)
259 , m_shadowColor(Color::transparent)
261 , m_globalComposite(CompositeSourceOver)
262 , m_globalBlend(WebBlendModeNormal)
263 , m_invertibleCTM(true)
264 , m_lineDashOffset(0)
265 , m_imageSmoothingEnabled(true)
266 , m_textAlign(StartTextAlign)
267 , m_textBaseline(AlphabeticTextBaseline)
268 , m_direction(DirectionInherit)
269 , m_unparsedFont(defaultFont)
270 , m_realizedFont(false)
275 CanvasRenderingContext2D::State::State(const State& other)
276 : CSSFontSelectorClient()
277 , m_unrealizedSaveCount(other.m_unrealizedSaveCount)
278 , m_unparsedStrokeColor(other.m_unparsedStrokeColor)
279 , m_unparsedFillColor(other.m_unparsedFillColor)
280 , m_strokeStyle(other.m_strokeStyle)
281 , m_fillStyle(other.m_fillStyle)
282 , m_lineWidth(other.m_lineWidth)
283 , m_lineCap(other.m_lineCap)
284 , m_lineJoin(other.m_lineJoin)
285 , m_miterLimit(other.m_miterLimit)
286 , m_shadowOffset(other.m_shadowOffset)
287 , m_shadowBlur(other.m_shadowBlur)
288 , m_shadowColor(other.m_shadowColor)
289 , m_globalAlpha(other.m_globalAlpha)
290 , m_globalComposite(other.m_globalComposite)
291 , m_globalBlend(other.m_globalBlend)
292 , m_transform(other.m_transform)
293 , m_invertibleCTM(other.m_invertibleCTM)
294 , m_lineDashOffset(other.m_lineDashOffset)
295 , m_imageSmoothingEnabled(other.m_imageSmoothingEnabled)
296 , m_textAlign(other.m_textAlign)
297 , m_textBaseline(other.m_textBaseline)
298 , m_direction(other.m_direction)
299 , m_unparsedFont(other.m_unparsedFont)
300 , m_font(other.m_font)
301 , m_realizedFont(other.m_realizedFont)
302 , m_hasClip(other.m_hasClip)
305 static_cast<CSSFontSelector*>(m_font.fontSelector())->registerForInvalidationCallbacks(this);
308 CanvasRenderingContext2D::State& CanvasRenderingContext2D::State::operator=(const State& other)
315 static_cast<CSSFontSelector*>(m_font.fontSelector())->unregisterForInvalidationCallbacks(this);
318 m_unrealizedSaveCount = other.m_unrealizedSaveCount;
319 m_unparsedStrokeColor = other.m_unparsedStrokeColor;
320 m_unparsedFillColor = other.m_unparsedFillColor;
321 m_strokeStyle = other.m_strokeStyle;
322 m_fillStyle = other.m_fillStyle;
323 m_lineWidth = other.m_lineWidth;
324 m_lineCap = other.m_lineCap;
325 m_lineJoin = other.m_lineJoin;
326 m_miterLimit = other.m_miterLimit;
327 m_shadowOffset = other.m_shadowOffset;
328 m_shadowBlur = other.m_shadowBlur;
329 m_shadowColor = other.m_shadowColor;
330 m_globalAlpha = other.m_globalAlpha;
331 m_globalComposite = other.m_globalComposite;
332 m_globalBlend = other.m_globalBlend;
333 m_transform = other.m_transform;
334 m_invertibleCTM = other.m_invertibleCTM;
335 m_imageSmoothingEnabled = other.m_imageSmoothingEnabled;
336 m_textAlign = other.m_textAlign;
337 m_textBaseline = other.m_textBaseline;
338 m_direction = other.m_direction;
339 m_unparsedFont = other.m_unparsedFont;
340 m_font = other.m_font;
341 m_realizedFont = other.m_realizedFont;
342 m_hasClip = other.m_hasClip;
345 static_cast<CSSFontSelector*>(m_font.fontSelector())->registerForInvalidationCallbacks(this);
350 CanvasRenderingContext2D::State::~State()
354 static_cast<CSSFontSelector*>(m_font.fontSelector())->unregisterForInvalidationCallbacks(this);
358 void CanvasRenderingContext2D::State::fontsNeedUpdate(CSSFontSelector* fontSelector)
360 ASSERT_ARG(fontSelector, fontSelector == m_font.fontSelector());
361 ASSERT(m_realizedFont);
363 m_font.update(fontSelector);
366 void CanvasRenderingContext2D::State::trace(Visitor* visitor)
368 visitor->trace(m_strokeStyle);
369 visitor->trace(m_fillStyle);
370 CSSFontSelectorClient::trace(visitor);
373 void CanvasRenderingContext2D::realizeSaves(GraphicsContext* context)
375 validateStateStack();
376 if (state().m_unrealizedSaveCount) {
377 ASSERT(m_stateStack.size() >= 1);
378 // Reduce the current state's unrealized count by one now,
379 // to reflect the fact we are saving one state.
380 m_stateStack.last()->m_unrealizedSaveCount--;
381 m_stateStack.append(adoptPtrWillBeNoop(new State(state())));
382 // Set the new state's unrealized count to 0, because it has no outstanding saves.
383 // We need to do this explicitly because the copy constructor and operator= used
384 // by the Vector operations copy the unrealized count from the previous state (in
385 // turn necessary to support correct resizing and unwinding of the stack).
386 m_stateStack.last()->m_unrealizedSaveCount = 0;
388 context = drawingContext();
391 validateStateStack();
395 void CanvasRenderingContext2D::restore()
397 validateStateStack();
398 if (state().m_unrealizedSaveCount) {
399 // We never realized the save, so just record that it was unnecessary.
400 --m_stateStack.last()->m_unrealizedSaveCount;
403 ASSERT(m_stateStack.size() >= 1);
404 if (m_stateStack.size() <= 1)
406 m_path.transform(state().m_transform);
407 m_stateStack.removeLast();
408 m_path.transform(state().m_transform.inverse());
409 GraphicsContext* c = drawingContext();
412 validateStateStack();
415 CanvasStyle* CanvasRenderingContext2D::strokeStyle() const
417 return state().m_strokeStyle.get();
420 void CanvasRenderingContext2D::setStrokeStyle(PassRefPtrWillBeRawPtr<CanvasStyle> prpStyle)
422 RefPtrWillBeRawPtr<CanvasStyle> style = prpStyle;
427 if (state().m_strokeStyle && state().m_strokeStyle->isEquivalentColor(*style))
430 if (style->isCurrentColor()) {
431 if (style->hasOverrideAlpha())
432 style = CanvasStyle::createFromRGBA(colorWithOverrideAlpha(currentColor(canvas()), style->overrideAlpha()));
434 style = CanvasStyle::createFromRGBA(currentColor(canvas()));
435 } else if (canvas()->originClean() && style->canvasPattern() && !style->canvasPattern()->originClean()) {
436 canvas()->setOriginTainted();
439 GraphicsContext* c = drawingContext();
441 modifiableState().m_strokeStyle = style.release();
444 state().m_strokeStyle->applyStrokeColor(c);
445 modifiableState().m_unparsedStrokeColor = String();
448 CanvasStyle* CanvasRenderingContext2D::fillStyle() const
450 return state().m_fillStyle.get();
453 void CanvasRenderingContext2D::setFillStyle(PassRefPtrWillBeRawPtr<CanvasStyle> prpStyle)
455 RefPtrWillBeRawPtr<CanvasStyle> style = prpStyle;
460 if (state().m_fillStyle && state().m_fillStyle->isEquivalentColor(*style))
463 if (style->isCurrentColor()) {
464 if (style->hasOverrideAlpha())
465 style = CanvasStyle::createFromRGBA(colorWithOverrideAlpha(currentColor(canvas()), style->overrideAlpha()));
467 style = CanvasStyle::createFromRGBA(currentColor(canvas()));
468 } else if (canvas()->originClean() && style->canvasPattern() && !style->canvasPattern()->originClean()) {
469 canvas()->setOriginTainted();
472 GraphicsContext* c = drawingContext();
474 modifiableState().m_fillStyle = style.release();
477 state().m_fillStyle->applyFillColor(c);
478 modifiableState().m_unparsedFillColor = String();
481 float CanvasRenderingContext2D::lineWidth() const
483 return state().m_lineWidth;
486 void CanvasRenderingContext2D::setLineWidth(float width)
488 if (!std::isfinite(width) || width <= 0)
490 if (state().m_lineWidth == width)
492 GraphicsContext* c = drawingContext();
494 modifiableState().m_lineWidth = width;
497 c->setStrokeThickness(width);
500 String CanvasRenderingContext2D::lineCap() const
502 return lineCapName(state().m_lineCap);
505 void CanvasRenderingContext2D::setLineCap(const String& s)
508 if (!parseLineCap(s, cap))
510 if (state().m_lineCap == cap)
512 GraphicsContext* c = drawingContext();
514 modifiableState().m_lineCap = cap;
520 String CanvasRenderingContext2D::lineJoin() const
522 return lineJoinName(state().m_lineJoin);
525 void CanvasRenderingContext2D::setLineJoin(const String& s)
528 if (!parseLineJoin(s, join))
530 if (state().m_lineJoin == join)
532 GraphicsContext* c = drawingContext();
534 modifiableState().m_lineJoin = join;
537 c->setLineJoin(join);
540 float CanvasRenderingContext2D::miterLimit() const
542 return state().m_miterLimit;
545 void CanvasRenderingContext2D::setMiterLimit(float limit)
547 if (!std::isfinite(limit) || limit <= 0)
549 if (state().m_miterLimit == limit)
551 GraphicsContext* c = drawingContext();
553 modifiableState().m_miterLimit = limit;
556 c->setMiterLimit(limit);
559 float CanvasRenderingContext2D::shadowOffsetX() const
561 return state().m_shadowOffset.width();
564 void CanvasRenderingContext2D::setShadowOffsetX(float x)
566 if (!std::isfinite(x))
568 if (state().m_shadowOffset.width() == x)
570 realizeSaves(nullptr);
571 modifiableState().m_shadowOffset.setWidth(x);
575 float CanvasRenderingContext2D::shadowOffsetY() const
577 return state().m_shadowOffset.height();
580 void CanvasRenderingContext2D::setShadowOffsetY(float y)
582 if (!std::isfinite(y))
584 if (state().m_shadowOffset.height() == y)
586 realizeSaves(nullptr);
587 modifiableState().m_shadowOffset.setHeight(y);
591 float CanvasRenderingContext2D::shadowBlur() const
593 return state().m_shadowBlur;
596 void CanvasRenderingContext2D::setShadowBlur(float blur)
598 if (!std::isfinite(blur) || blur < 0)
600 if (state().m_shadowBlur == blur)
602 realizeSaves(nullptr);
603 modifiableState().m_shadowBlur = blur;
607 String CanvasRenderingContext2D::shadowColor() const
609 return Color(state().m_shadowColor).serialized();
612 void CanvasRenderingContext2D::setShadowColor(const String& color)
615 if (!parseColorOrCurrentColor(rgba, color, canvas()))
617 if (state().m_shadowColor == rgba)
619 realizeSaves(nullptr);
620 modifiableState().m_shadowColor = rgba;
624 const Vector<float>& CanvasRenderingContext2D::getLineDash() const
626 return state().m_lineDash;
629 static bool lineDashSequenceIsValid(const Vector<float>& dash)
631 for (size_t i = 0; i < dash.size(); i++) {
632 if (!std::isfinite(dash[i]) || dash[i] < 0)
638 void CanvasRenderingContext2D::setLineDash(const Vector<float>& dash)
640 if (!lineDashSequenceIsValid(dash))
643 realizeSaves(nullptr);
644 modifiableState().m_lineDash = dash;
645 // Spec requires the concatenation of two copies the dash list when the
646 // number of elements is odd
648 modifiableState().m_lineDash.appendVector(dash);
653 float CanvasRenderingContext2D::lineDashOffset() const
655 return state().m_lineDashOffset;
658 void CanvasRenderingContext2D::setLineDashOffset(float offset)
660 if (!std::isfinite(offset) || state().m_lineDashOffset == offset)
663 realizeSaves(nullptr);
664 modifiableState().m_lineDashOffset = offset;
668 void CanvasRenderingContext2D::applyLineDash() const
670 GraphicsContext* c = drawingContext();
673 DashArray convertedLineDash(state().m_lineDash.size());
674 for (size_t i = 0; i < state().m_lineDash.size(); ++i)
675 convertedLineDash[i] = static_cast<DashArrayElement>(state().m_lineDash[i]);
676 c->setLineDash(convertedLineDash, state().m_lineDashOffset);
679 float CanvasRenderingContext2D::globalAlpha() const
681 return state().m_globalAlpha;
684 void CanvasRenderingContext2D::setGlobalAlpha(float alpha)
686 if (!(alpha >= 0 && alpha <= 1))
688 if (state().m_globalAlpha == alpha)
690 GraphicsContext* c = drawingContext();
692 modifiableState().m_globalAlpha = alpha;
695 c->setAlphaAsFloat(alpha);
698 String CanvasRenderingContext2D::globalCompositeOperation() const
700 return compositeOperatorName(state().m_globalComposite, state().m_globalBlend);
703 void CanvasRenderingContext2D::setGlobalCompositeOperation(const String& operation)
705 CompositeOperator op = CompositeSourceOver;
706 WebBlendMode blendMode = WebBlendModeNormal;
707 if (!parseCompositeAndBlendOperator(operation, op, blendMode))
709 // crbug.com/425628: Count the use of "darker" to remove it.
710 if (op == CompositePlusDarker)
711 UseCounter::count(canvas()->document(), UseCounter::CanvasRenderingContext2DCompositeOperationDarker);
712 if ((state().m_globalComposite == op) && (state().m_globalBlend == blendMode))
714 GraphicsContext* c = drawingContext();
716 modifiableState().m_globalComposite = op;
717 modifiableState().m_globalBlend = blendMode;
720 c->setCompositeOperation(op, blendMode);
723 void CanvasRenderingContext2D::setCurrentTransform(PassRefPtr<SVGMatrixTearOff> passMatrixTearOff)
725 RefPtr<SVGMatrixTearOff> matrixTearOff = passMatrixTearOff;
726 const AffineTransform& transform = matrixTearOff->value();
727 setTransform(transform.a(), transform.b(), transform.c(), transform.d(), transform.e(), transform.f());
730 void CanvasRenderingContext2D::scale(float sx, float sy)
732 GraphicsContext* c = drawingContext();
735 if (!state().m_invertibleCTM)
738 if (!std::isfinite(sx) || !std::isfinite(sy))
741 AffineTransform newTransform = state().m_transform;
742 newTransform.scaleNonUniform(sx, sy);
743 if (state().m_transform == newTransform)
748 if (!newTransform.isInvertible()) {
749 modifiableState().m_invertibleCTM = false;
753 modifiableState().m_transform = newTransform;
755 m_path.transform(AffineTransform().scaleNonUniform(1.0 / sx, 1.0 / sy));
758 void CanvasRenderingContext2D::rotate(float angleInRadians)
760 GraphicsContext* c = drawingContext();
763 if (!state().m_invertibleCTM)
766 if (!std::isfinite(angleInRadians))
769 AffineTransform newTransform = state().m_transform;
770 newTransform.rotateRadians(angleInRadians);
771 if (state().m_transform == newTransform)
776 if (!newTransform.isInvertible()) {
777 modifiableState().m_invertibleCTM = false;
781 modifiableState().m_transform = newTransform;
782 c->rotate(angleInRadians);
783 m_path.transform(AffineTransform().rotateRadians(-angleInRadians));
786 void CanvasRenderingContext2D::translate(float tx, float ty)
788 GraphicsContext* c = drawingContext();
791 if (!state().m_invertibleCTM)
794 if (!std::isfinite(tx) || !std::isfinite(ty))
797 AffineTransform newTransform = state().m_transform;
798 newTransform.translate(tx, ty);
799 if (state().m_transform == newTransform)
804 if (!newTransform.isInvertible()) {
805 modifiableState().m_invertibleCTM = false;
809 modifiableState().m_transform = newTransform;
810 c->translate(tx, ty);
811 m_path.transform(AffineTransform().translate(-tx, -ty));
814 void CanvasRenderingContext2D::transform(float m11, float m12, float m21, float m22, float dx, float dy)
816 GraphicsContext* c = drawingContext();
819 if (!state().m_invertibleCTM)
822 if (!std::isfinite(m11) || !std::isfinite(m21) || !std::isfinite(dx) || !std::isfinite(m12) || !std::isfinite(m22) || !std::isfinite(dy))
825 AffineTransform transform(m11, m12, m21, m22, dx, dy);
826 AffineTransform newTransform = state().m_transform * transform;
827 if (state().m_transform == newTransform)
832 modifiableState().m_transform = newTransform;
833 if (!newTransform.isInvertible()) {
834 modifiableState().m_invertibleCTM = false;
838 c->concatCTM(transform);
839 m_path.transform(transform.inverse());
842 void CanvasRenderingContext2D::resetTransform()
844 GraphicsContext* c = drawingContext();
848 AffineTransform ctm = state().m_transform;
849 bool invertibleCTM = state().m_invertibleCTM;
850 // It is possible that CTM is identity while CTM is not invertible.
851 // When CTM becomes non-invertible, realizeSaves() can make CTM identity.
852 if (ctm.isIdentity() && invertibleCTM)
856 // resetTransform() resolves the non-invertible CTM state.
857 modifiableState().m_transform.makeIdentity();
858 modifiableState().m_invertibleCTM = true;
859 c->setCTM(canvas()->baseTransform());
862 m_path.transform(ctm);
863 // When else, do nothing because all transform methods didn't update m_path when CTM became non-invertible.
864 // It means that resetTransform() restores m_path just before CTM became non-invertible.
867 void CanvasRenderingContext2D::setTransform(float m11, float m12, float m21, float m22, float dx, float dy)
869 GraphicsContext* c = drawingContext();
873 if (!std::isfinite(m11) || !std::isfinite(m21) || !std::isfinite(dx) || !std::isfinite(m12) || !std::isfinite(m22) || !std::isfinite(dy))
877 transform(m11, m12, m21, m22, dx, dy);
880 void CanvasRenderingContext2D::setStrokeColor(const String& color)
882 if (color == state().m_unparsedStrokeColor)
884 realizeSaves(nullptr);
885 setStrokeStyle(CanvasStyle::createFromString(color));
886 modifiableState().m_unparsedStrokeColor = color;
889 void CanvasRenderingContext2D::setStrokeColor(float grayLevel)
891 if (state().m_strokeStyle && state().m_strokeStyle->isEquivalentRGBA(grayLevel, grayLevel, grayLevel, 1.0f))
893 setStrokeStyle(CanvasStyle::createFromGrayLevelWithAlpha(grayLevel, 1.0f));
896 void CanvasRenderingContext2D::setStrokeColor(const String& color, float alpha)
898 setStrokeStyle(CanvasStyle::createFromStringWithOverrideAlpha(color, alpha));
901 void CanvasRenderingContext2D::setStrokeColor(float grayLevel, float alpha)
903 if (state().m_strokeStyle && state().m_strokeStyle->isEquivalentRGBA(grayLevel, grayLevel, grayLevel, alpha))
905 setStrokeStyle(CanvasStyle::createFromGrayLevelWithAlpha(grayLevel, alpha));
908 void CanvasRenderingContext2D::setStrokeColor(float r, float g, float b, float a)
910 if (state().m_strokeStyle && state().m_strokeStyle->isEquivalentRGBA(r, g, b, a))
912 setStrokeStyle(CanvasStyle::createFromRGBAChannels(r, g, b, a));
915 void CanvasRenderingContext2D::setStrokeColor(float c, float m, float y, float k, float a)
917 if (state().m_strokeStyle && state().m_strokeStyle->isEquivalentCMYKA(c, m, y, k, a))
919 setStrokeStyle(CanvasStyle::createFromCMYKAChannels(c, m, y, k, a));
922 void CanvasRenderingContext2D::setFillColor(const String& color)
924 if (color == state().m_unparsedFillColor)
926 realizeSaves(nullptr);
927 setFillStyle(CanvasStyle::createFromString(color));
928 modifiableState().m_unparsedFillColor = color;
931 void CanvasRenderingContext2D::setFillColor(float grayLevel)
933 if (state().m_fillStyle && state().m_fillStyle->isEquivalentRGBA(grayLevel, grayLevel, grayLevel, 1.0f))
935 setFillStyle(CanvasStyle::createFromGrayLevelWithAlpha(grayLevel, 1.0f));
938 void CanvasRenderingContext2D::setFillColor(const String& color, float alpha)
940 setFillStyle(CanvasStyle::createFromStringWithOverrideAlpha(color, alpha));
943 void CanvasRenderingContext2D::setFillColor(float grayLevel, float alpha)
945 if (state().m_fillStyle && state().m_fillStyle->isEquivalentRGBA(grayLevel, grayLevel, grayLevel, alpha))
947 setFillStyle(CanvasStyle::createFromGrayLevelWithAlpha(grayLevel, alpha));
950 void CanvasRenderingContext2D::setFillColor(float r, float g, float b, float a)
952 if (state().m_fillStyle && state().m_fillStyle->isEquivalentRGBA(r, g, b, a))
954 setFillStyle(CanvasStyle::createFromRGBAChannels(r, g, b, a));
957 void CanvasRenderingContext2D::setFillColor(float c, float m, float y, float k, float a)
959 if (state().m_fillStyle && state().m_fillStyle->isEquivalentCMYKA(c, m, y, k, a))
961 setFillStyle(CanvasStyle::createFromCMYKAChannels(c, m, y, k, a));
964 void CanvasRenderingContext2D::beginPath()
969 static bool validateRectForCanvas(float& x, float& y, float& width, float& height)
971 if (!std::isfinite(x) || !std::isfinite(y) || !std::isfinite(width) || !std::isfinite(height))
974 if (!width && !height)
990 static bool isFullCanvasCompositeMode(CompositeOperator op)
992 // See 4.8.11.1.3 Compositing
993 // CompositeSourceAtop and CompositeDestinationOut are not listed here as the platforms already
994 // implement the specification's behavior.
995 return op == CompositeSourceIn || op == CompositeSourceOut || op == CompositeDestinationIn || op == CompositeDestinationAtop;
998 static WindRule parseWinding(const String& windingRuleString)
1000 if (windingRuleString == "nonzero")
1001 return RULE_NONZERO;
1002 if (windingRuleString == "evenodd")
1003 return RULE_EVENODD;
1005 ASSERT_NOT_REACHED();
1006 return RULE_EVENODD;
1009 void CanvasRenderingContext2D::fillInternal(const Path& path, const String& windingRuleString)
1011 if (path.isEmpty()) {
1014 GraphicsContext* c = drawingContext();
1018 if (!state().m_invertibleCTM) {
1021 FloatRect clipBounds;
1022 if (!c->getTransformedClipBounds(&clipBounds)) {
1026 // If gradient size is zero, then paint nothing.
1027 Gradient* gradient = c->fillGradient();
1028 if (gradient && gradient->isZeroSize()) {
1032 WindRule windRule = c->fillRule();
1033 c->setFillRule(parseWinding(windingRuleString));
1035 if (isFullCanvasCompositeMode(state().m_globalComposite)) {
1036 fullCanvasCompositedDraw(bind(&GraphicsContext::fillPath, c, path));
1037 didDraw(clipBounds);
1038 } else if (state().m_globalComposite == CompositeCopy) {
1042 applyShadow(DrawShadowAndForeground);
1043 didDraw(clipBounds);
1045 FloatRect dirtyRect;
1046 if (computeDirtyRect(path.boundingRect(), clipBounds, &dirtyRect)) {
1052 c->setFillRule(windRule);
1055 void CanvasRenderingContext2D::fill(const String& windingRuleString)
1057 fillInternal(m_path, windingRuleString);
1060 void CanvasRenderingContext2D::fill(Path2D* domPath, const String& windingRuleString)
1062 fillInternal(domPath->path(), windingRuleString);
1065 void CanvasRenderingContext2D::strokeInternal(const Path& path)
1067 if (path.isEmpty()) {
1070 GraphicsContext* c = drawingContext();
1074 if (!state().m_invertibleCTM) {
1077 FloatRect clipBounds;
1078 if (!c->getTransformedClipBounds(&clipBounds))
1081 // If gradient size is zero, then paint nothing.
1082 Gradient* gradient = c->strokeGradient();
1083 if (gradient && gradient->isZeroSize()) {
1087 if (isFullCanvasCompositeMode(state().m_globalComposite)) {
1088 fullCanvasCompositedDraw(bind(&GraphicsContext::strokePath, c, path));
1089 didDraw(clipBounds);
1090 } else if (state().m_globalComposite == CompositeCopy) {
1093 c->strokePath(path);
1094 applyShadow(DrawShadowAndForeground);
1095 didDraw(clipBounds);
1097 FloatRect bounds = path.boundingRect();
1098 inflateStrokeRect(bounds);
1099 FloatRect dirtyRect;
1100 if (computeDirtyRect(bounds, clipBounds, &dirtyRect)) {
1101 c->strokePath(path);
1107 void CanvasRenderingContext2D::stroke()
1109 strokeInternal(m_path);
1112 void CanvasRenderingContext2D::stroke(Path2D* domPath)
1114 strokeInternal(domPath->path());
1117 void CanvasRenderingContext2D::clipInternal(const Path& path, const String& windingRuleString)
1119 GraphicsContext* c = drawingContext();
1123 if (!state().m_invertibleCTM) {
1128 c->canvasClip(path, parseWinding(windingRuleString), m_clipAntialiasing);
1129 modifiableState().m_hasClip = true;
1132 void CanvasRenderingContext2D::clip(const String& windingRuleString)
1134 clipInternal(m_path, windingRuleString);
1137 void CanvasRenderingContext2D::clip(Path2D* domPath, const String& windingRuleString)
1139 clipInternal(domPath->path(), windingRuleString);
1142 bool CanvasRenderingContext2D::isPointInPath(const float x, const float y, const String& windingRuleString)
1144 return isPointInPathInternal(m_path, x, y, windingRuleString);
1147 bool CanvasRenderingContext2D::isPointInPath(Path2D* domPath, const float x, const float y, const String& windingRuleString)
1149 return isPointInPathInternal(domPath->path(), x, y, windingRuleString);
1152 bool CanvasRenderingContext2D::isPointInPathInternal(const Path& path, const float x, const float y, const String& windingRuleString)
1154 GraphicsContext* c = drawingContext();
1157 if (!state().m_invertibleCTM)
1160 FloatPoint point(x, y);
1161 if (!std::isfinite(point.x()) || !std::isfinite(point.y()))
1163 AffineTransform ctm = state().m_transform;
1164 FloatPoint transformedPoint = ctm.inverse().mapPoint(point);
1166 return path.contains(transformedPoint, parseWinding(windingRuleString));
1169 bool CanvasRenderingContext2D::isPointInStroke(const float x, const float y)
1171 return isPointInStrokeInternal(m_path, x, y);
1174 bool CanvasRenderingContext2D::isPointInStroke(Path2D* domPath, const float x, const float y)
1176 return isPointInStrokeInternal(domPath->path(), x, y);
1179 bool CanvasRenderingContext2D::isPointInStrokeInternal(const Path& path, const float x, const float y)
1181 GraphicsContext* c = drawingContext();
1184 if (!state().m_invertibleCTM)
1187 FloatPoint point(x, y);
1188 if (!std::isfinite(point.x()) || !std::isfinite(point.y()))
1190 AffineTransform ctm = state().m_transform;
1191 FloatPoint transformedPoint = ctm.inverse().mapPoint(point);
1193 StrokeData strokeData;
1194 strokeData.setThickness(lineWidth());
1195 strokeData.setLineCap(getLineCap());
1196 strokeData.setLineJoin(getLineJoin());
1197 strokeData.setMiterLimit(miterLimit());
1198 strokeData.setLineDash(getLineDash(), lineDashOffset());
1199 return path.strokeContains(transformedPoint, strokeData);
1202 void CanvasRenderingContext2D::scrollPathIntoView()
1204 scrollPathIntoViewInternal(m_path);
1207 void CanvasRenderingContext2D::scrollPathIntoView(Path2D* path2d)
1209 scrollPathIntoViewInternal(path2d->path());
1212 void CanvasRenderingContext2D::scrollPathIntoViewInternal(const Path& path)
1214 RenderObject* renderer = canvas()->renderer();
1215 RenderBox* renderBox = canvas()->renderBox();
1216 if (!renderer || !renderBox || !state().m_invertibleCTM || path.isEmpty())
1219 canvas()->document().updateLayoutIgnorePendingStylesheets();
1221 // Apply transformation and get the bounding rect
1222 Path transformedPath = path;
1223 transformedPath.transform(state().m_transform);
1224 FloatRect boundingRect = transformedPath.boundingRect();
1226 // Offset by the canvas rect
1227 LayoutRect pathRect(boundingRect);
1228 IntRect canvasRect = renderBox->absoluteContentBox();
1229 pathRect.move(canvasRect.x(), canvasRect.y());
1231 renderer->scrollRectToVisible(
1232 pathRect, ScrollAlignment::alignCenterAlways, ScrollAlignment::alignTopAlways);
1234 // TODO: should implement "inform the user" that the caret and/or
1235 // selection the specified rectangle of the canvas. See http://crbug.com/357987
1238 void CanvasRenderingContext2D::clearRect(float x, float y, float width, float height)
1240 if (!validateRectForCanvas(x, y, width, height))
1242 GraphicsContext* context = drawingContext();
1245 if (!state().m_invertibleCTM)
1247 FloatRect rect(x, y, width, height);
1249 FloatRect dirtyRect;
1250 if (!computeDirtyRect(rect, &dirtyRect))
1254 if (shouldDrawShadows()) {
1257 context->clearShadow();
1259 if (state().m_globalAlpha != 1) {
1264 context->setAlphaAsFloat(1);
1266 if (state().m_globalComposite != CompositeSourceOver) {
1271 context->setCompositeOperation(CompositeSourceOver);
1273 context->clearRect(rect);
1274 if (m_hitRegionManager)
1275 m_hitRegionManager->removeHitRegionsInRect(rect, state().m_transform);
1279 validateStateStack();
1283 // FIXME(crbug.com/425531): Funtional.h cannot handle override function signature.
1284 static void fillRectOnContext(GraphicsContext* context, const FloatRect& rect)
1286 context->fillRect(rect);
1289 static void strokeRectOnContext(GraphicsContext* context, const FloatRect& rect)
1291 context->strokeRect(rect);
1294 void CanvasRenderingContext2D::fillRect(float x, float y, float width, float height)
1296 if (!validateRectForCanvas(x, y, width, height))
1299 GraphicsContext* c = drawingContext();
1302 if (!state().m_invertibleCTM)
1304 FloatRect clipBounds;
1305 if (!c->getTransformedClipBounds(&clipBounds))
1308 // from the HTML5 Canvas spec:
1309 // If x0 = x1 and y0 = y1, then the linear gradient must paint nothing
1310 // If x0 = x1 and y0 = y1 and r0 = r1, then the radial gradient must paint nothing
1311 Gradient* gradient = c->fillGradient();
1312 if (gradient && gradient->isZeroSize())
1315 FloatRect rect(x, y, width, height);
1316 if (rectContainsTransformedRect(rect, clipBounds)) {
1318 didDraw(clipBounds);
1319 } else if (isFullCanvasCompositeMode(state().m_globalComposite)) {
1320 fullCanvasCompositedDraw(bind(&fillRectOnContext, c, rect));
1321 didDraw(clipBounds);
1322 } else if (state().m_globalComposite == CompositeCopy) {
1326 applyShadow(DrawShadowAndForeground);
1327 didDraw(clipBounds);
1329 FloatRect dirtyRect;
1330 if (computeDirtyRect(rect, clipBounds, &dirtyRect)) {
1337 void CanvasRenderingContext2D::strokeRect(float x, float y, float width, float height)
1339 if (!validateRectForCanvas(x, y, width, height))
1342 if (!(state().m_lineWidth >= 0))
1345 GraphicsContext* c = drawingContext();
1348 if (!state().m_invertibleCTM)
1350 FloatRect clipBounds;
1351 if (!c->getTransformedClipBounds(&clipBounds))
1354 // If gradient size is zero, then paint nothing.
1355 Gradient* gradient = c->strokeGradient();
1356 if (gradient && gradient->isZeroSize())
1359 FloatRect rect(x, y, width, height);
1360 if (isFullCanvasCompositeMode(state().m_globalComposite)) {
1361 fullCanvasCompositedDraw(bind(&strokeRectOnContext, c, rect));
1362 didDraw(clipBounds);
1363 } else if (state().m_globalComposite == CompositeCopy) {
1366 c->strokeRect(rect);
1367 applyShadow(DrawShadowAndForeground);
1368 didDraw(clipBounds);
1370 FloatRect boundingRect = rect;
1371 boundingRect.inflate(state().m_lineWidth / 2);
1372 FloatRect dirtyRect;
1373 if (computeDirtyRect(boundingRect, clipBounds, &dirtyRect)) {
1374 c->strokeRect(rect);
1380 void CanvasRenderingContext2D::setShadow(float width, float height, float blur)
1382 setShadow(FloatSize(width, height), blur, Color::transparent);
1385 void CanvasRenderingContext2D::setShadow(float width, float height, float blur, const String& color)
1388 if (!parseColorOrCurrentColor(rgba, color, canvas()))
1390 setShadow(FloatSize(width, height), blur, rgba);
1393 void CanvasRenderingContext2D::setShadow(float width, float height, float blur, float grayLevel)
1395 setShadow(FloatSize(width, height), blur, makeRGBA32FromFloats(grayLevel, grayLevel, grayLevel, 1));
1398 void CanvasRenderingContext2D::setShadow(float width, float height, float blur, const String& color, float alpha)
1401 if (!parseColorOrCurrentColor(rgba, color, canvas()))
1403 setShadow(FloatSize(width, height), blur, colorWithOverrideAlpha(rgba, alpha));
1406 void CanvasRenderingContext2D::setShadow(float width, float height, float blur, float grayLevel, float alpha)
1408 setShadow(FloatSize(width, height), blur, makeRGBA32FromFloats(grayLevel, grayLevel, grayLevel, alpha));
1411 void CanvasRenderingContext2D::setShadow(float width, float height, float blur, float r, float g, float b, float a)
1413 setShadow(FloatSize(width, height), blur, makeRGBA32FromFloats(r, g, b, a));
1416 void CanvasRenderingContext2D::setShadow(float width, float height, float blur, float c, float m, float y, float k, float a)
1418 setShadow(FloatSize(width, height), blur, makeRGBAFromCMYKA(c, m, y, k, a));
1421 void CanvasRenderingContext2D::clearShadow()
1423 setShadow(FloatSize(), 0, Color::transparent);
1426 void CanvasRenderingContext2D::setShadow(const FloatSize& offset, float blur, RGBA32 color)
1428 if (state().m_shadowOffset == offset && state().m_shadowBlur == blur && state().m_shadowColor == color)
1430 bool wasDrawingShadows = shouldDrawShadows();
1431 realizeSaves(nullptr);
1432 modifiableState().m_shadowOffset = offset;
1433 modifiableState().m_shadowBlur = blur;
1434 modifiableState().m_shadowColor = color;
1435 if (!wasDrawingShadows && !shouldDrawShadows())
1440 void CanvasRenderingContext2D::applyShadow(ShadowMode shadowMode)
1442 GraphicsContext* c = drawingContext();
1446 if (shouldDrawShadows()) {
1447 c->setShadow(state().m_shadowOffset, state().m_shadowBlur, state().m_shadowColor,
1448 DrawLooperBuilder::ShadowIgnoresTransforms, DrawLooperBuilder::ShadowRespectsAlpha, shadowMode);
1454 bool CanvasRenderingContext2D::shouldDrawShadows() const
1456 return alphaChannel(state().m_shadowColor) && (state().m_shadowBlur || !state().m_shadowOffset.isZero());
1459 static inline FloatRect normalizeRect(const FloatRect& rect)
1461 return FloatRect(std::min(rect.x(), rect.maxX()),
1462 std::min(rect.y(), rect.maxY()),
1463 std::max(rect.width(), -rect.width()),
1464 std::max(rect.height(), -rect.height()));
1467 static inline void clipRectsToImageRect(const FloatRect& imageRect, FloatRect* srcRect, FloatRect* dstRect)
1469 if (imageRect.contains(*srcRect))
1472 // Compute the src to dst transform
1473 FloatSize scale(dstRect->size().width() / srcRect->size().width(), dstRect->size().height() / srcRect->size().height());
1474 FloatPoint scaledSrcLocation = srcRect->location();
1475 scaledSrcLocation.scale(scale.width(), scale.height());
1476 FloatSize offset = dstRect->location() - scaledSrcLocation;
1478 srcRect->intersect(imageRect);
1480 // To clip the destination rectangle in the same proportion, transform the clipped src rect
1481 *dstRect = *srcRect;
1482 dstRect->scale(scale.width(), scale.height());
1483 dstRect->move(offset);
1486 void CanvasRenderingContext2D::drawImage(CanvasImageSource* imageSource, float x, float y, ExceptionState& exceptionState)
1488 FloatSize sourceRectSize = imageSource->sourceSize();
1489 FloatSize destRectSize = imageSource->defaultDestinationSize();
1490 drawImageInternal(imageSource, 0, 0, sourceRectSize.width(), sourceRectSize.height(), x, y, destRectSize.width(), destRectSize.height(), exceptionState);
1493 void CanvasRenderingContext2D::drawImage(CanvasImageSource* imageSource,
1494 float x, float y, float width, float height, ExceptionState& exceptionState)
1496 FloatSize sourceRectSize = imageSource->sourceSize();
1497 drawImageInternal(imageSource, 0, 0, sourceRectSize.width(), sourceRectSize.height(), x, y, width, height, exceptionState);
1500 void CanvasRenderingContext2D::drawImage(CanvasImageSource* imageSource,
1501 float sx, float sy, float sw, float sh,
1502 float dx, float dy, float dw, float dh, ExceptionState& exceptionState)
1504 drawImageInternal(imageSource, sx, sy, sw, sh, dx, dy, dw, dh, exceptionState);
1507 static void drawVideo(GraphicsContext* c, CanvasImageSource* imageSource, FloatRect srcRect, FloatRect dstRect)
1509 HTMLVideoElement* video = static_cast<HTMLVideoElement*>(imageSource);
1510 GraphicsContextStateSaver stateSaver(*c);
1512 c->translate(dstRect.x(), dstRect.y());
1513 c->scale(dstRect.width() / srcRect.width(), dstRect.height() / srcRect.height());
1514 c->translate(-srcRect.x(), -srcRect.y());
1515 video->paintCurrentFrameInContext(c, IntRect(IntPoint(), IntSize(video->videoWidth(), video->videoHeight())));
1516 stateSaver.restore();
1519 static void drawImageOnContext(GraphicsContext* c, CanvasImageSource* imageSource, Image* image, const FloatRect& srcRect, const FloatRect& dstRect)
1521 if (!imageSource->isVideoElement()) {
1522 c->drawImage(image, dstRect, srcRect, c->compositeOperation(), c->blendModeOperation());
1524 drawVideo(c, static_cast<HTMLVideoElement*>(imageSource), srcRect, dstRect);
1528 void CanvasRenderingContext2D::drawImageInternal(CanvasImageSource* imageSource,
1529 float sx, float sy, float sw, float sh,
1530 float dx, float dy, float dw, float dh, ExceptionState& exceptionState)
1532 RefPtr<Image> image;
1533 SourceImageStatus sourceImageStatus = InvalidSourceImageStatus;
1534 if (!imageSource->isVideoElement()) {
1535 SourceImageMode mode = canvas() == imageSource ? CopySourceImageIfVolatile : DontCopySourceImage; // Thunking for ==
1536 image = imageSource->getSourceImageForCanvas(mode, &sourceImageStatus);
1537 if (sourceImageStatus == UndecodableSourceImageStatus)
1538 exceptionState.throwDOMException(InvalidStateError, "The HTMLImageElement provided is in the 'broken' state.");
1539 if (!image || !image->width() || !image->height())
1543 GraphicsContext* c = drawingContext();
1547 if (!state().m_invertibleCTM)
1550 if (!std::isfinite(dx) || !std::isfinite(dy) || !std::isfinite(dw) || !std::isfinite(dh)
1551 || !std::isfinite(sx) || !std::isfinite(sy) || !std::isfinite(sw) || !std::isfinite(sh)
1552 || !dw || !dh || !sw || !sh)
1555 FloatRect clipBounds;
1556 if (!c->getTransformedClipBounds(&clipBounds))
1559 FloatRect srcRect = normalizeRect(FloatRect(sx, sy, sw, sh));
1560 FloatRect dstRect = normalizeRect(FloatRect(dx, dy, dw, dh));
1562 clipRectsToImageRect(FloatRect(FloatPoint(), imageSource->sourceSize()), &srcRect, &dstRect);
1564 imageSource->adjustDrawRects(&srcRect, &dstRect);
1566 if (srcRect.isEmpty())
1569 if (imageSource->isVideoElement())
1570 canvas()->buffer()->willDrawVideo();
1572 CompositeOperator op = state().m_globalComposite;
1573 if (rectContainsTransformedRect(dstRect, clipBounds)) {
1574 drawImageOnContext(c, imageSource, image.get(), srcRect, dstRect);
1575 didDraw(clipBounds);
1576 } else if (isFullCanvasCompositeMode(op)) {
1577 fullCanvasCompositedDraw(bind(&drawImageOnContext, c, imageSource, image.get(), srcRect, dstRect));
1578 didDraw(clipBounds);
1579 } else if (op == CompositeCopy) {
1581 drawImageOnContext(c, imageSource, image.get(), srcRect, dstRect);
1582 didDraw(clipBounds);
1584 FloatRect dirtyRect;
1585 if (computeDirtyRect(dstRect, clipBounds, &dirtyRect)) {
1586 drawImageOnContext(c, imageSource, image.get(), srcRect, dstRect);
1590 validateStateStack();
1592 if (sourceImageStatus == ExternalSourceImageStatus && isAccelerated() && canvas()->buffer())
1593 canvas()->buffer()->flush();
1595 if (canvas()->originClean() && wouldTaintOrigin(imageSource))
1596 canvas()->setOriginTainted();
1599 void CanvasRenderingContext2D::drawImageFromRect(HTMLImageElement* image,
1600 float sx, float sy, float sw, float sh,
1601 float dx, float dy, float dw, float dh,
1602 const String& compositeOperation)
1607 setGlobalCompositeOperation(compositeOperation);
1608 drawImageInternal(image, sx, sy, sw, sh, dx, dy, dw, dh, IGNORE_EXCEPTION);
1612 void CanvasRenderingContext2D::setAlpha(float alpha)
1614 setGlobalAlpha(alpha);
1617 void CanvasRenderingContext2D::setCompositeOperation(const String& operation)
1619 setGlobalCompositeOperation(operation);
1622 void CanvasRenderingContext2D::clearCanvas()
1624 FloatRect canvasRect(0, 0, canvas()->width(), canvas()->height());
1625 GraphicsContext* c = drawingContext();
1630 c->setCTM(canvas()->baseTransform());
1631 c->clearRect(canvasRect);
1635 bool CanvasRenderingContext2D::rectContainsTransformedRect(const FloatRect& rect, const FloatRect& transformedRect) const
1637 FloatQuad quad(rect);
1638 FloatQuad transformedQuad(transformedRect);
1639 return state().m_transform.mapQuad(quad).containsQuad(transformedQuad);
1642 void CanvasRenderingContext2D::fullCanvasCompositedDraw(const Closure& draw)
1644 ASSERT(isFullCanvasCompositeMode(state().m_globalComposite));
1646 GraphicsContext* c = drawingContext();
1649 CompositeOperator previousOperator = c->compositeOperation();
1650 if (shouldDrawShadows()) {
1651 // unroll into two independently composited passes if drawing shadows
1652 c->beginLayer(1, state().m_globalComposite);
1653 c->setCompositeOperation(CompositeSourceOver);
1654 applyShadow(DrawShadowOnly);
1656 c->setCompositeOperation(previousOperator);
1660 c->beginLayer(1, state().m_globalComposite);
1662 c->setCompositeOperation(CompositeSourceOver);
1664 c->setCompositeOperation(previousOperator);
1666 applyShadow(DrawShadowAndForeground); // go back to normal shadows mode
1669 PassRefPtrWillBeRawPtr<CanvasGradient> CanvasRenderingContext2D::createLinearGradient(float x0, float y0, float x1, float y1)
1671 RefPtrWillBeRawPtr<CanvasGradient> gradient = CanvasGradient::create(FloatPoint(x0, y0), FloatPoint(x1, y1));
1672 return gradient.release();
1675 PassRefPtrWillBeRawPtr<CanvasGradient> CanvasRenderingContext2D::createRadialGradient(float x0, float y0, float r0, float x1, float y1, float r1, ExceptionState& exceptionState)
1677 if (r0 < 0 || r1 < 0) {
1678 exceptionState.throwDOMException(IndexSizeError, String::format("The %s provided is less than 0.", r0 < 0 ? "r0" : "r1"));
1682 RefPtrWillBeRawPtr<CanvasGradient> gradient = CanvasGradient::create(FloatPoint(x0, y0), r0, FloatPoint(x1, y1), r1);
1683 return gradient.release();
1686 PassRefPtrWillBeRawPtr<CanvasPattern> CanvasRenderingContext2D::createPattern(CanvasImageSource* imageSource,
1687 const String& repetitionType, ExceptionState& exceptionState)
1689 Pattern::RepeatMode repeatMode = CanvasPattern::parseRepetitionType(repetitionType, exceptionState);
1690 if (exceptionState.hadException())
1693 SourceImageStatus status;
1694 RefPtr<Image> imageForRendering = imageSource->getSourceImageForCanvas(CopySourceImageIfVolatile, &status);
1697 case NormalSourceImageStatus:
1699 case ZeroSizeCanvasSourceImageStatus:
1700 exceptionState.throwDOMException(InvalidStateError, String::format("The canvas %s is 0.", imageSource->sourceSize().width() ? "height" : "width"));
1702 case UndecodableSourceImageStatus:
1703 exceptionState.throwDOMException(InvalidStateError, "Source image is in the 'broken' state.");
1705 case InvalidSourceImageStatus:
1706 imageForRendering = Image::nullImage();
1708 case IncompleteSourceImageStatus:
1711 case ExternalSourceImageStatus: // should not happen when mode is CopySourceImageIfVolatile
1712 ASSERT_NOT_REACHED();
1715 ASSERT(imageForRendering);
1717 bool originClean = !wouldTaintOrigin(imageSource);
1719 return CanvasPattern::create(imageForRendering.release(), repeatMode, originClean);
1722 bool CanvasRenderingContext2D::computeDirtyRect(const FloatRect& localRect, FloatRect* dirtyRect)
1724 FloatRect clipBounds;
1725 if (!drawingContext()->getTransformedClipBounds(&clipBounds))
1727 return computeDirtyRect(localRect, clipBounds, dirtyRect);
1730 bool CanvasRenderingContext2D::computeDirtyRect(const FloatRect& localRect, const FloatRect& transformedClipBounds, FloatRect* dirtyRect)
1732 FloatRect canvasRect = state().m_transform.mapRect(localRect);
1734 if (alphaChannel(state().m_shadowColor)) {
1735 FloatRect shadowRect(canvasRect);
1736 shadowRect.move(state().m_shadowOffset);
1737 shadowRect.inflate(state().m_shadowBlur);
1738 canvasRect.unite(shadowRect);
1741 canvasRect.intersect(transformedClipBounds);
1742 if (canvasRect.isEmpty())
1746 *dirtyRect = canvasRect;
1751 void CanvasRenderingContext2D::didDraw(const FloatRect& dirtyRect)
1753 if (dirtyRect.isEmpty())
1756 canvas()->didDraw(dirtyRect);
1759 GraphicsContext* CanvasRenderingContext2D::drawingContext() const
1761 if (isContextLost())
1763 return canvas()->drawingContext();
1766 static PassRefPtrWillBeRawPtr<ImageData> createEmptyImageData(const IntSize& size)
1768 if (RefPtrWillBeRawPtr<ImageData> data = ImageData::create(size)) {
1769 data->data()->zeroFill();
1770 return data.release();
1776 PassRefPtrWillBeRawPtr<ImageData> CanvasRenderingContext2D::createImageData(PassRefPtrWillBeRawPtr<ImageData> imageData) const
1778 return createEmptyImageData(imageData->size());
1781 PassRefPtrWillBeRawPtr<ImageData> CanvasRenderingContext2D::createImageData(float sw, float sh, ExceptionState& exceptionState) const
1784 exceptionState.throwDOMException(IndexSizeError, String::format("The source %s is 0.", sw ? "height" : "width"));
1788 FloatSize logicalSize(fabs(sw), fabs(sh));
1789 if (!logicalSize.isExpressibleAsIntSize())
1792 IntSize size = expandedIntSize(logicalSize);
1793 if (size.width() < 1)
1795 if (size.height() < 1)
1798 return createEmptyImageData(size);
1801 PassRefPtrWillBeRawPtr<ImageData> CanvasRenderingContext2D::getImageData(float sx, float sy, float sw, float sh, ExceptionState& exceptionState) const
1803 if (!canvas()->originClean())
1804 exceptionState.throwSecurityError("The canvas has been tainted by cross-origin data.");
1805 else if (!sw || !sh)
1806 exceptionState.throwDOMException(IndexSizeError, String::format("The source %s is 0.", sw ? "height" : "width"));
1808 if (exceptionState.hadException())
1820 FloatRect logicalRect(sx, sy, sw, sh);
1821 if (logicalRect.width() < 1)
1822 logicalRect.setWidth(1);
1823 if (logicalRect.height() < 1)
1824 logicalRect.setHeight(1);
1825 if (!logicalRect.isExpressibleAsIntRect())
1828 IntRect imageDataRect = enclosingIntRect(logicalRect);
1829 ImageBuffer* buffer = canvas()->buffer();
1830 if (!buffer || isContextLost())
1831 return createEmptyImageData(imageDataRect.size());
1833 RefPtr<Uint8ClampedArray> byteArray = buffer->getImageData(Unmultiplied, imageDataRect);
1837 return ImageData::create(imageDataRect.size(), byteArray.release());
1840 void CanvasRenderingContext2D::putImageData(ImageData* data, float dx, float dy)
1842 putImageData(data, dx, dy, 0, 0, data->width(), data->height());
1845 void CanvasRenderingContext2D::putImageData(ImageData* data, float dx, float dy, float dirtyX, float dirtyY, float dirtyWidth, float dirtyHeight)
1847 ImageBuffer* buffer = canvas()->buffer();
1851 if (dirtyWidth < 0) {
1852 dirtyX += dirtyWidth;
1853 dirtyWidth = -dirtyWidth;
1856 if (dirtyHeight < 0) {
1857 dirtyY += dirtyHeight;
1858 dirtyHeight = -dirtyHeight;
1861 FloatRect clipRect(dirtyX, dirtyY, dirtyWidth, dirtyHeight);
1862 clipRect.intersect(IntRect(0, 0, data->width(), data->height()));
1863 IntSize destOffset(static_cast<int>(dx), static_cast<int>(dy));
1864 IntRect destRect = enclosingIntRect(clipRect);
1865 destRect.move(destOffset);
1866 destRect.intersect(IntRect(IntPoint(), buffer->size()));
1867 if (destRect.isEmpty())
1869 IntRect sourceRect(destRect);
1870 sourceRect.move(-destOffset);
1872 buffer->putByteArray(Unmultiplied, data->data(), IntSize(data->width(), data->height()), sourceRect, IntPoint(destOffset));
1877 String CanvasRenderingContext2D::font() const
1879 if (!state().m_realizedFont)
1882 StringBuilder serializedFont;
1883 const FontDescription& fontDescription = state().m_font.fontDescription();
1885 if (fontDescription.style() == FontStyleItalic)
1886 serializedFont.appendLiteral("italic ");
1887 if (fontDescription.weight() == FontWeightBold)
1888 serializedFont.appendLiteral("bold ");
1889 if (fontDescription.variant() == FontVariantSmallCaps)
1890 serializedFont.appendLiteral("small-caps ");
1892 serializedFont.appendNumber(fontDescription.computedPixelSize());
1893 serializedFont.appendLiteral("px");
1895 const FontFamily& firstFontFamily = fontDescription.family();
1896 for (const FontFamily* fontFamily = &firstFontFamily; fontFamily; fontFamily = fontFamily->next()) {
1897 if (fontFamily != &firstFontFamily)
1898 serializedFont.append(',');
1900 // FIXME: We should append family directly to serializedFont rather than building a temporary string.
1901 String family = fontFamily->family();
1902 if (family.startsWith("-webkit-"))
1903 family = family.substring(8);
1904 if (family.contains(' '))
1905 family = "\"" + family + "\"";
1907 serializedFont.append(' ');
1908 serializedFont.append(family);
1911 return serializedFont.toString();
1914 void CanvasRenderingContext2D::setFont(const String& newFont)
1916 // The style resolution required for rendering text is not available in frame-less documents.
1917 if (!canvas()->document().frame())
1920 RefPtrWillBeRawPtr<MutableStylePropertySet> parsedStyle;
1921 MutableStylePropertyMap::iterator i = m_fetchedFonts.find(newFont);
1922 if (i != m_fetchedFonts.end()) {
1923 parsedStyle = i->value;
1924 m_fetchedFontsLRUList.remove(newFont);
1926 parsedStyle = MutableStylePropertySet::create();
1927 CSSParserMode mode = m_usesCSSCompatibilityParseMode ? HTMLQuirksMode : HTMLStandardMode;
1928 CSSParser::parseValue(parsedStyle.get(), CSSPropertyFont, newFont, true, mode, 0);
1929 if (m_fetchedFonts.size() >= FetchedFontsCacheLimit) {
1930 m_fetchedFonts.remove(m_fetchedFontsLRUList.first());
1931 m_fetchedFontsLRUList.removeFirst();
1933 m_fetchedFonts.add(newFont, parsedStyle);
1935 m_fetchedFontsLRUList.add(newFont);
1937 if (parsedStyle->isEmpty())
1940 String fontValue = parsedStyle->getPropertyValue(CSSPropertyFont);
1942 // According to http://lists.w3.org/Archives/Public/public-html/2009Jul/0947.html,
1943 // the "inherit" and "initial" values must be ignored.
1944 if (fontValue == "inherit" || fontValue == "initial")
1947 // The parse succeeded.
1948 String newFontSafeCopy(newFont); // Create a string copy since newFont can be deleted inside realizeSaves.
1949 realizeSaves(nullptr);
1950 modifiableState().m_unparsedFont = newFontSafeCopy;
1952 // Map the <canvas> font into the text style. If the font uses keywords like larger/smaller, these will work
1953 // relative to the canvas.
1954 RefPtr<RenderStyle> newStyle = RenderStyle::create();
1955 canvas()->document().updateRenderTreeIfNeeded();
1956 if (RenderStyle* computedStyle = canvas()->computedStyle()) {
1957 FontDescription elementFontDescription(computedStyle->fontDescription());
1958 // Reset the computed size to avoid inheriting the zoom factor from the <canvas> element.
1959 elementFontDescription.setComputedSize(elementFontDescription.specifiedSize());
1960 newStyle->setFontDescription(elementFontDescription);
1962 FontFamily fontFamily;
1963 fontFamily.setFamily(defaultFontFamily);
1965 FontDescription defaultFontDescription;
1966 defaultFontDescription.setFamily(fontFamily);
1967 defaultFontDescription.setSpecifiedSize(defaultFontSize);
1968 defaultFontDescription.setComputedSize(defaultFontSize);
1970 newStyle->setFontDescription(defaultFontDescription);
1973 newStyle->font().update(newStyle->font().fontSelector());
1975 // Now map the font property longhands into the style.
1976 CSSPropertyValue properties[] = {
1977 CSSPropertyValue(CSSPropertyFontFamily, *parsedStyle),
1978 CSSPropertyValue(CSSPropertyFontStyle, *parsedStyle),
1979 CSSPropertyValue(CSSPropertyFontVariant, *parsedStyle),
1980 CSSPropertyValue(CSSPropertyFontWeight, *parsedStyle),
1981 CSSPropertyValue(CSSPropertyFontSize, *parsedStyle),
1982 CSSPropertyValue(CSSPropertyLineHeight, *parsedStyle),
1985 StyleResolver& styleResolver = canvas()->document().ensureStyleResolver();
1986 styleResolver.applyPropertiesToStyle(properties, WTF_ARRAY_LENGTH(properties), newStyle.get());
1989 if (state().m_realizedFont)
1990 static_cast<CSSFontSelector*>(state().m_font.fontSelector())->unregisterForInvalidationCallbacks(&modifiableState());
1992 modifiableState().m_font = newStyle->font();
1993 modifiableState().m_font.update(canvas()->document().styleEngine()->fontSelector());
1994 modifiableState().m_realizedFont = true;
1995 canvas()->document().styleEngine()->fontSelector()->registerForInvalidationCallbacks(&modifiableState());
1998 String CanvasRenderingContext2D::textAlign() const
2000 return textAlignName(state().m_textAlign);
2003 void CanvasRenderingContext2D::setTextAlign(const String& s)
2006 if (!parseTextAlign(s, align))
2008 if (state().m_textAlign == align)
2010 realizeSaves(nullptr);
2011 modifiableState().m_textAlign = align;
2014 String CanvasRenderingContext2D::textBaseline() const
2016 return textBaselineName(state().m_textBaseline);
2019 void CanvasRenderingContext2D::setTextBaseline(const String& s)
2021 TextBaseline baseline;
2022 if (!parseTextBaseline(s, baseline))
2024 if (state().m_textBaseline == baseline)
2026 realizeSaves(nullptr);
2027 modifiableState().m_textBaseline = baseline;
2030 inline TextDirection CanvasRenderingContext2D::toTextDirection(Direction direction, RenderStyle** computedStyle) const
2032 RenderStyle* style = (computedStyle || direction == DirectionInherit) ? canvas()->computedStyle() : nullptr;
2034 *computedStyle = style;
2035 switch (direction) {
2036 case DirectionInherit:
2037 return style ? style->direction() : LTR;
2043 ASSERT_NOT_REACHED();
2047 String CanvasRenderingContext2D::direction() const
2049 if (state().m_direction == DirectionInherit)
2050 canvas()->document().updateRenderTreeIfNeeded();
2051 return toTextDirection(state().m_direction) == RTL ? rtl : ltr;
2054 void CanvasRenderingContext2D::setDirection(const String& directionString)
2056 Direction direction;
2057 if (directionString == inherit)
2058 direction = DirectionInherit;
2059 else if (directionString == rtl)
2060 direction = DirectionRTL;
2061 else if (directionString == ltr)
2062 direction = DirectionLTR;
2066 if (state().m_direction == direction)
2069 realizeSaves(nullptr);
2070 modifiableState().m_direction = direction;
2073 void CanvasRenderingContext2D::fillText(const String& text, float x, float y)
2075 drawTextInternal(text, x, y, true);
2078 void CanvasRenderingContext2D::fillText(const String& text, float x, float y, float maxWidth)
2080 drawTextInternal(text, x, y, true, maxWidth, true);
2083 void CanvasRenderingContext2D::strokeText(const String& text, float x, float y)
2085 drawTextInternal(text, x, y, false);
2088 void CanvasRenderingContext2D::strokeText(const String& text, float x, float y, float maxWidth)
2090 drawTextInternal(text, x, y, false, maxWidth, true);
2093 PassRefPtrWillBeRawPtr<TextMetrics> CanvasRenderingContext2D::measureText(const String& text)
2095 RefPtrWillBeRawPtr<TextMetrics> metrics = TextMetrics::create();
2097 // The style resolution required for rendering text is not available in frame-less documents.
2098 if (!canvas()->document().frame())
2099 return metrics.release();
2101 FontCachePurgePreventer fontCachePurgePreventer;
2102 canvas()->document().updateRenderTreeIfNeeded();
2103 const Font& font = accessFont();
2104 const TextRun textRun(text, 0, 0, TextRun::AllowTrailingExpansion | TextRun::ForbidLeadingExpansion, LTR, false, true, true);
2105 FloatRect textBounds = font.selectionRectForText(textRun, FloatPoint(), font.fontDescription().computedSize(), 0, -1, true);
2108 metrics->setWidth(font.width(textRun));
2109 metrics->setActualBoundingBoxLeft(-textBounds.x());
2110 metrics->setActualBoundingBoxRight(textBounds.maxX());
2113 const FontMetrics& fontMetrics = font.fontMetrics();
2114 const float ascent = fontMetrics.floatAscent();
2115 const float descent = fontMetrics.floatDescent();
2116 const float baselineY = getFontBaseline(fontMetrics);
2118 metrics->setFontBoundingBoxAscent(ascent - baselineY);
2119 metrics->setFontBoundingBoxDescent(descent + baselineY);
2120 metrics->setActualBoundingBoxAscent(-textBounds.y() - baselineY);
2121 metrics->setActualBoundingBoxDescent(textBounds.maxY() + baselineY);
2123 // Note : top/bottom and ascend/descend are currently the same, so there's no difference
2124 // between the EM box's top and bottom and the font's ascend and descend
2125 metrics->setEmHeightAscent(0);
2126 metrics->setEmHeightDescent(0);
2128 metrics->setHangingBaseline(-0.8f * ascent + baselineY);
2129 metrics->setAlphabeticBaseline(baselineY);
2130 metrics->setIdeographicBaseline(descent + baselineY);
2131 return metrics.release();
2134 void CanvasRenderingContext2D::drawTextInternal(const String& text, float x, float y, bool fill, float maxWidth, bool useMaxWidth)
2136 // The style resolution required for rendering text is not available in frame-less documents.
2137 if (!canvas()->document().frame())
2140 // accessFont needs the style to be up to date, but updating style can cause script to run,
2141 // (e.g. due to autofocus) which can free the GraphicsContext, so update style before grabbing
2142 // the GraphicsContext.
2143 canvas()->document().updateRenderTreeIfNeeded();
2145 GraphicsContext* c = drawingContext();
2148 if (!state().m_invertibleCTM)
2150 if (!std::isfinite(x) || !std::isfinite(y))
2152 if (useMaxWidth && (!std::isfinite(maxWidth) || maxWidth <= 0))
2155 // If gradient size is zero, then paint nothing.
2156 Gradient* gradient = c->strokeGradient();
2157 if (!fill && gradient && gradient->isZeroSize())
2160 gradient = c->fillGradient();
2161 if (fill && gradient && gradient->isZeroSize())
2164 FontCachePurgePreventer fontCachePurgePreventer;
2166 const Font& font = accessFont();
2167 const FontMetrics& fontMetrics = font.fontMetrics();
2169 // FIXME: Need to turn off font smoothing.
2171 RenderStyle* computedStyle;
2172 TextDirection direction = toTextDirection(state().m_direction, &computedStyle);
2173 bool isRTL = direction == RTL;
2174 bool override = computedStyle ? isOverride(computedStyle->unicodeBidi()) : false;
2176 TextRun textRun(text, 0, 0, TextRun::AllowTrailingExpansion, direction, override, true, true);
2177 // Draw the item text at the correct point.
2178 FloatPoint location(x, y + getFontBaseline(fontMetrics));
2179 float fontWidth = font.width(textRun);
2181 useMaxWidth = (useMaxWidth && maxWidth < fontWidth);
2182 float width = useMaxWidth ? maxWidth : fontWidth;
2184 TextAlign align = state().m_textAlign;
2185 if (align == StartTextAlign)
2186 align = isRTL ? RightTextAlign : LeftTextAlign;
2187 else if (align == EndTextAlign)
2188 align = isRTL ? LeftTextAlign : RightTextAlign;
2191 case CenterTextAlign:
2192 location.setX(location.x() - width / 2);
2194 case RightTextAlign:
2195 location.setX(location.x() - width);
2201 // The slop built in to this mask rect matches the heuristic used in FontCGWin.cpp for GDI text.
2202 TextRunPaintInfo textRunPaintInfo(textRun);
2203 textRunPaintInfo.bounds = FloatRect(location.x() - fontMetrics.height() / 2,
2204 location.y() - fontMetrics.ascent() - fontMetrics.lineGap(),
2205 width + fontMetrics.height(),
2206 fontMetrics.lineSpacing());
2208 inflateStrokeRect(textRunPaintInfo.bounds);
2210 c->setTextDrawingMode(fill ? TextModeFill : TextModeStroke);
2212 GraphicsContextStateSaver stateSaver(*c);
2214 c->translate(location.x(), location.y());
2215 // We draw when fontWidth is 0 so compositing operations (eg, a "copy" op) still work.
2216 c->scale((fontWidth > 0 ? (width / fontWidth) : 0), 1);
2217 location = FloatPoint();
2220 FloatRect clipBounds;
2221 if (!c->getTransformedClipBounds(&clipBounds)) {
2225 if (isFullCanvasCompositeMode(state().m_globalComposite)) {
2226 fullCanvasCompositedDraw(bind(&GraphicsContext::drawBidiText, c, font, textRunPaintInfo, location, Font::UseFallbackIfFontNotReady));
2227 didDraw(clipBounds);
2228 } else if (state().m_globalComposite == CompositeCopy) {
2231 c->drawBidiText(font, textRunPaintInfo, location, Font::UseFallbackIfFontNotReady);
2232 applyShadow(DrawShadowAndForeground);
2233 didDraw(clipBounds);
2235 FloatRect dirtyRect;
2236 if (computeDirtyRect(textRunPaintInfo.bounds, clipBounds, &dirtyRect)) {
2237 c->drawBidiText(font, textRunPaintInfo, location, Font::UseFallbackIfFontNotReady);
2243 void CanvasRenderingContext2D::inflateStrokeRect(FloatRect& rect) const
2245 // Fast approximation of the stroke's bounding rect.
2246 // This yields a slightly oversized rect but is very fast
2247 // compared to Path::strokeBoundingRect().
2248 static const float root2 = sqrtf(2);
2249 float delta = state().m_lineWidth / 2;
2250 if (state().m_lineJoin == MiterJoin)
2251 delta *= state().m_miterLimit;
2252 else if (state().m_lineCap == SquareCap)
2255 rect.inflate(delta);
2258 const Font& CanvasRenderingContext2D::accessFont()
2260 // This needs style to be up to date, but can't assert so because drawTextInternal
2261 // can invalidate style before this is called (e.g. drawingContext invalidates style).
2262 if (!state().m_realizedFont)
2263 setFont(state().m_unparsedFont);
2264 return state().m_font;
2267 int CanvasRenderingContext2D::getFontBaseline(const FontMetrics& fontMetrics) const
2269 switch (state().m_textBaseline) {
2270 case TopTextBaseline:
2271 return fontMetrics.ascent();
2272 case HangingTextBaseline:
2273 // According to http://wiki.apache.org/xmlgraphics-fop/LineLayout/AlignmentHandling
2274 // "FOP (Formatting Objects Processor) puts the hanging baseline at 80% of the ascender height"
2275 return (fontMetrics.ascent() * 4) / 5;
2276 case BottomTextBaseline:
2277 case IdeographicTextBaseline:
2278 return -fontMetrics.descent();
2279 case MiddleTextBaseline:
2280 return -fontMetrics.descent() + fontMetrics.height() / 2;
2281 case AlphabeticTextBaseline:
2289 void CanvasRenderingContext2D::setIsHidden(bool hidden)
2291 ImageBuffer* buffer = canvas()->buffer();
2293 buffer->setIsHidden(hidden);
2296 WebLayer* CanvasRenderingContext2D::platformLayer() const
2298 return canvas()->buffer() ? canvas()->buffer()->platformLayer() : 0;
2301 bool CanvasRenderingContext2D::imageSmoothingEnabled() const
2303 return state().m_imageSmoothingEnabled;
2306 void CanvasRenderingContext2D::setImageSmoothingEnabled(bool enabled)
2308 if (enabled == state().m_imageSmoothingEnabled)
2311 GraphicsContext* c = drawingContext();
2313 modifiableState().m_imageSmoothingEnabled = enabled;
2315 c->setImageInterpolationQuality(enabled ? CanvasDefaultInterpolationQuality : InterpolationNone);
2318 PassRefPtrWillBeRawPtr<Canvas2DContextAttributes> CanvasRenderingContext2D::getContextAttributes() const
2320 RefPtrWillBeRawPtr<Canvas2DContextAttributes> attributes = Canvas2DContextAttributes::create();
2321 attributes->setAlpha(m_hasAlpha);
2322 return attributes.release();
2325 void CanvasRenderingContext2D::drawFocusIfNeeded(Element* element)
2327 drawFocusIfNeededInternal(m_path, element);
2330 void CanvasRenderingContext2D::drawFocusIfNeeded(Path2D* path2d, Element* element)
2332 drawFocusIfNeededInternal(path2d->path(), element);
2335 void CanvasRenderingContext2D::drawFocusIfNeededInternal(const Path& path, Element* element)
2337 if (!focusRingCallIsValid(path, element))
2340 // Note: we need to check document->focusedElement() rather than just calling
2341 // element->focused(), because element->focused() isn't updated until after
2342 // focus events fire.
2343 if (element->document().focusedElement() == element)
2344 drawFocusRing(path);
2347 bool CanvasRenderingContext2D::focusRingCallIsValid(const Path& path, Element* element)
2350 if (!state().m_invertibleCTM)
2354 if (!element->isDescendantOf(canvas()))
2360 void CanvasRenderingContext2D::drawFocusRing(const Path& path)
2362 GraphicsContext* c = drawingContext();
2366 // These should match the style defined in html.css.
2367 Color focusRingColor = RenderTheme::theme().focusRingColor();
2368 const int focusRingWidth = 5;
2369 const int focusRingOutline = 0;
2371 // We need to add focusRingWidth to dirtyRect.
2372 StrokeData strokeData;
2373 strokeData.setThickness(focusRingWidth);
2375 FloatRect dirtyRect;
2376 if (!computeDirtyRect(path.strokeBoundingRect(strokeData), &dirtyRect))
2380 c->setAlphaAsFloat(1.0);
2382 c->setCompositeOperation(CompositeSourceOver, WebBlendModeNormal);
2383 c->drawFocusRing(path, focusRingWidth, focusRingOutline, focusRingColor);
2385 validateStateStack();
2389 void CanvasRenderingContext2D::addHitRegion(const HitRegionOptions& options, ExceptionState& exceptionState)
2391 if (options.id().isEmpty() && !options.control()) {
2392 exceptionState.throwDOMException(NotSupportedError, "Both id and control are null.");
2396 Path hitRegionPath = options.hasPath() ? options.path()->path() : m_path;
2398 FloatRect clipBounds;
2399 GraphicsContext* context = drawingContext();
2401 if (hitRegionPath.isEmpty() || !context || !state().m_invertibleCTM
2402 || !context->getTransformedClipBounds(&clipBounds)) {
2403 exceptionState.throwDOMException(NotSupportedError, "The specified path has no pixels.");
2407 hitRegionPath.transform(state().m_transform);
2410 // FIXME: The hit regions should take clipping region into account.
2411 // However, we have no way to get the region from canvas state stack by now.
2412 // See http://crbug.com/387057
2413 exceptionState.throwDOMException(NotSupportedError, "The specified path has no pixels.");
2417 if (!m_hitRegionManager)
2418 m_hitRegionManager = HitRegionManager::create();
2420 // Remove previous region (with id or control)
2421 m_hitRegionManager->removeHitRegionById(options.id());
2422 m_hitRegionManager->removeHitRegionByControl(options.control().get());
2424 RefPtrWillBeRawPtr<HitRegion> hitRegion = HitRegion::create(hitRegionPath, options);
2425 hitRegion->updateAccessibility(canvas());
2426 m_hitRegionManager->addHitRegion(hitRegion.release());
2429 void CanvasRenderingContext2D::removeHitRegion(const String& id)
2431 if (m_hitRegionManager)
2432 m_hitRegionManager->removeHitRegionById(id);
2435 void CanvasRenderingContext2D::clearHitRegions()
2437 if (m_hitRegionManager)
2438 m_hitRegionManager->removeAllHitRegions();
2441 HitRegion* CanvasRenderingContext2D::hitRegionAtPoint(const LayoutPoint& point)
2443 if (m_hitRegionManager)
2444 return m_hitRegionManager->getHitRegionAtPoint(point);
2449 unsigned CanvasRenderingContext2D::hitRegionsCount() const
2451 if (m_hitRegionManager)
2452 return m_hitRegionManager->getHitRegionsCount();
2457 } // namespace blink