2 * Copyright (C) 2011 University of Szeged
3 * Copyright (C) 2011 Renata Hodovan <reni@webkit.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY UNIVERSITY OF SZEGED ``AS IS'' AND ANY
16 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL UNIVERSITY OF SZEGED OR
19 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
23 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include "core/rendering/svg/RenderSVGRect.h"
31 #include "platform/graphics/GraphicsContext.h"
35 RenderSVGRect::RenderSVGRect(SVGRectElement* node)
36 : RenderSVGShape(node)
37 , m_usePathFallback(false)
41 RenderSVGRect::~RenderSVGRect()
45 void RenderSVGRect::updateShapeFromElement()
47 // Before creating a new object we need to clear the cached bounding box
48 // to avoid using garbage.
49 m_fillBoundingBox = FloatRect();
50 m_innerStrokeRect = FloatRect();
51 m_outerStrokeRect = FloatRect();
52 SVGRectElement* rect = toSVGRectElement(element());
55 SVGLengthContext lengthContext(rect);
56 FloatSize boundingBoxSize(rect->width()->currentValue()->value(lengthContext), rect->height()->currentValue()->value(lengthContext));
58 // Spec: "A negative value is an error."
59 if (boundingBoxSize.width() < 0 || boundingBoxSize.height() < 0)
62 // Spec: "A value of zero disables rendering of the element."
63 if (!boundingBoxSize.isEmpty()) {
64 // Fallback to RenderSVGShape if rect has rounded corners or a non-scaling stroke.
65 if (rect->rx()->currentValue()->value(lengthContext) > 0 || rect->ry()->currentValue()->value(lengthContext) > 0 || hasNonScalingStroke()) {
66 RenderSVGShape::updateShapeFromElement();
67 m_usePathFallback = true;
70 m_usePathFallback = false;
73 m_fillBoundingBox = FloatRect(FloatPoint(rect->x()->currentValue()->value(lengthContext), rect->y()->currentValue()->value(lengthContext)), boundingBoxSize);
75 // To decide if the stroke contains a point we create two rects which represent the inner and
76 // the outer stroke borders. A stroke contains the point, if the point is between them.
77 m_innerStrokeRect = m_fillBoundingBox;
78 m_outerStrokeRect = m_fillBoundingBox;
80 if (style()->svgStyle().hasStroke()) {
81 float strokeWidth = this->strokeWidth();
82 m_innerStrokeRect.inflate(-strokeWidth / 2);
83 m_outerStrokeRect.inflate(strokeWidth / 2);
86 m_strokeBoundingBox = m_outerStrokeRect;
89 void RenderSVGRect::fillShape(GraphicsContext* context) const
91 if (m_usePathFallback) {
92 RenderSVGShape::fillShape(context);
96 context->fillRect(m_fillBoundingBox);
99 void RenderSVGRect::strokeShape(GraphicsContext* context) const
101 if (!style()->svgStyle().hasVisibleStroke())
104 if (m_usePathFallback) {
105 RenderSVGShape::strokeShape(context);
109 context->strokeRect(m_fillBoundingBox, strokeWidth());
112 bool RenderSVGRect::shapeDependentStrokeContains(const FloatPoint& point)
114 // The optimized contains code below does not support non-smooth strokes so we need
115 // to fall back to RenderSVGShape::shapeDependentStrokeContains in these cases.
116 if (m_usePathFallback || !hasSmoothStroke()) {
118 RenderSVGShape::updateShapeFromElement();
119 return RenderSVGShape::shapeDependentStrokeContains(point);
122 return m_outerStrokeRect.contains(point, FloatRect::InsideOrOnStroke) && !m_innerStrokeRect.contains(point, FloatRect::InsideButNotOnStroke);
125 bool RenderSVGRect::shapeDependentFillContains(const FloatPoint& point, const WindRule fillRule) const
127 if (m_usePathFallback)
128 return RenderSVGShape::shapeDependentFillContains(point, fillRule);
129 return m_fillBoundingBox.contains(point.x(), point.y());